mail-notification

Fork of Jean-Yves Lefort's mail-notification, a tray icon to notify of new mail
git clone https://code.djc.id.au/git/mail-notification/

src/mn-sound-file-chooser-dialog.gob (3754B) - raw

      1 /*
      2  * Mail Notification
      3  * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation; either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License along
     16  * with this program; if not, write to the Free Software Foundation, Inc.,
     17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     18  */
     19 
     20 %headertop{
     21 #include <gtk/gtk.h>
     22 %}
     23 
     24 %privateheader{
     25 #include "mn-sound-player.h"
     26 %}
     27 
     28 %{
     29 #include <glib/gi18n.h>
     30 
     31 enum
     32 {
     33   RESPONSE_PLAY = 1,
     34   RESPONSE_STOP
     35 };
     36 %}
     37 
     38 class MN:Sound:File:Chooser:Dialog from Gtk:File:Chooser:Dialog
     39 {
     40   private MNSoundPlayer *sound_player unrefwith g_object_unref;
     41 
     42   constructor (self)
     43   {
     44     GtkFileFilter *filter;
     45 
     46     selfp->sound_player = mn_sound_player_new();
     47 
     48     gtk_window_set_title(GTK_WINDOW(self), _("Select a File"));
     49 
     50     gtk_dialog_add_buttons(GTK_DIALOG(self),
     51 			   GTK_STOCK_MEDIA_PLAY, RESPONSE_PLAY,
     52 			   GTK_STOCK_MEDIA_STOP, RESPONSE_STOP,
     53 			   GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
     54 			   GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
     55 			   NULL);
     56 
     57     filter = gtk_file_filter_new();
     58     gtk_file_filter_set_name(filter, _("All files"));
     59     gtk_file_filter_add_pattern(filter, "*");
     60     gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(self), filter);
     61 
     62     filter = gtk_file_filter_new();
     63     gtk_file_filter_set_name(filter, _("Audio files"));
     64     gtk_file_filter_add_mime_type(filter, "audio/*");
     65     gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(self), filter);
     66     gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(self), filter);
     67 
     68     g_object_connect(self,
     69 		     "signal::response", self_response_h, NULL,
     70 		     "signal::selection-changed", self_update_sensitivity, NULL,
     71 		     NULL);
     72 
     73     g_object_connect(selfp->sound_player,
     74 		     "swapped-signal::notify::can-play", self_update_sensitivity, self,
     75 		     "swapped-signal::notify::can-stop", self_update_sensitivity, self,
     76 		     NULL);
     77 
     78     self_update_sensitivity(self);
     79   }
     80 
     81   private void
     82     response_h (GtkDialog *dialog, int response, gpointer user_data)
     83   {
     84     Self *self = SELF(dialog);
     85 
     86     if (response == RESPONSE_PLAY)
     87       {
     88 	char *filename;
     89 
     90 	filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
     91 	if (filename)
     92 	  {
     93 	    mn_sound_player_play(selfp->sound_player, filename, GTK_WINDOW(dialog));
     94 	    g_free(filename);
     95 	  }
     96       }
     97     else if (response == RESPONSE_STOP)
     98       mn_sound_player_stop(selfp->sound_player);
     99     else
    100       return;
    101 
    102     /*
    103      * Do not pass our internal responses to client code and do not
    104      * let the dialog be destroyed.
    105      */
    106     g_signal_stop_emission_by_name(dialog, "response");
    107   }
    108 
    109   private void
    110     update_sensitivity (self)
    111   {
    112     gtk_dialog_set_response_sensitive(GTK_DIALOG(self), RESPONSE_PLAY, self_has_selected_file(self) && mn_sound_player_get_can_play(selfp->sound_player));
    113     gtk_dialog_set_response_sensitive(GTK_DIALOG(self), RESPONSE_STOP, mn_sound_player_get_can_stop(selfp->sound_player));
    114   }
    115 
    116   private gboolean
    117     has_selected_file (self)
    118   {
    119     char *filename;
    120     gboolean has;
    121 
    122     filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(self));
    123     has = filename && ! g_file_test(filename, G_FILE_TEST_IS_DIR);
    124     g_free(filename);
    125 
    126     return has;
    127   }
    128 
    129   public GtkWidget *
    130     new (void)
    131   {
    132     return GTK_WIDGET(GET_NEW);
    133   }
    134 }