]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Finito l'invio degli allegati in SolipaMailUI (closes #96).
authorAndrea Zagli <azagli@libero.it>
Tue, 15 Nov 2011 10:58:43 +0000 (11:58 +0100)
committerAndrea Zagli <azagli@libero.it>
Tue, 15 Nov 2011 10:58:43 +0000 (11:58 +0100)
data/libsolipa/gui/libsolipa.ui
src/mailui.c

index 91ebc064456725454193ce89fc71f82b64eaed27..c3a307019f598c883abe95e4364d2b24f677910d 100644 (file)
                           <object class="GtkIconView" id="iconview1">
                             <property name="visible">True</property>
                             <property name="can_focus">True</property>
+                            <property name="selection_mode">multiple</property>
                             <property name="model">lstore_mail_attachments</property>
                           </object>
                         </child>
                             <property name="label">gtk-remove</property>
                             <property name="use_action_appearance">False</property>
                             <property name="visible">True</property>
+                            <property name="sensitive">False</property>
                             <property name="can_focus">True</property>
                             <property name="receives_default">True</property>
                             <property name="use_stock">True</property>
index 3e04c1cf66532a402a6588613e18d2aa57c7f687..c56a7806d0a86b42dc5ef91196f1bbc9f0ce8507 100644 (file)
@@ -40,7 +40,8 @@ enum
        PROP_SUBJECT,
        PROP_SUBJECT_EDITABLE,
        PROP_BODY,
-       PROP_BODY_EDITABLE
+       PROP_BODY_EDITABLE,
+       PROP_WITH_ATTACHMENTS
 };
 
 enum
@@ -62,6 +63,9 @@ static void solipa_mail_ui_get_property (GObject *object,
                                GValue *value,
                                GParamSpec *pspec);
 
+static void solipa_mail_ui_on_iv_attachment_selection_changed  (GtkIconView *iconview,
+                                                                gpointer user_data);
+
 static void solipa_mail_ui_on_btn_attachment_add_clicked (GtkButton *button,
                                                           gpointer user_data);
 static void solipa_mail_ui_on_btn_attachment_remove_clicked (GtkButton *button,
@@ -94,6 +98,7 @@ struct _SolipaMailUIPrivate
                GtkWidget *subject;
                GtkWidget *body;
                GtkWidget *iv_attachments;
+               GtkWidget *btn_attachment_remove;
 
                GtkListStore *lstore_attachments;
        };
@@ -213,6 +218,13 @@ solipa_mail_ui_class_init (SolipaMailUIClass *klass)
                                                               TRUE,
                                                               G_PARAM_READWRITE));
 
+       g_object_class_install_property (object_class, PROP_WITH_ATTACHMENTS,
+                                        g_param_spec_boolean ("with-attachments",
+                                                              "With attachments",
+                                                              "With attachments",
+                                                              TRUE,
+                                                              G_PARAM_READWRITE));
+
        /**
         * SolipaMailUI::btn_cancel_clicked:
         * @solipa_mail_ui:
@@ -312,12 +324,16 @@ SolipaMailUI
 
        priv->lstore_attachments = GTK_LIST_STORE (gtk_builder_get_object (builder, "lstore_mail_attachments"));
        priv->iv_attachments = GTK_WIDGET (gtk_builder_get_object (builder, "iconview1"));
+       priv->btn_attachment_remove = GTK_WIDGET (gtk_builder_get_object (builder, "button10"));
 
-       gtk_icon_view_set_text_column (GTK_ICON_VIEW (priv->iv_attachments), COL_ATTACHMENTS_FILE_NAME);
+       gtk_icon_view_set_text_column (GTK_ICON_VIEW (priv->iv_attachments), COL_ATTACHMENTS_FILE_NAME_BASE);
+       gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->iv_attachments), COL_ATTACHMENTS_ICON);
 
+       g_signal_connect (gtk_builder_get_object (builder, "iconview1"), "selection-changed",
+                         G_CALLBACK (solipa_mail_ui_on_iv_attachment_selection_changed), (gpointer)solipa_mail_ui);
        g_signal_connect (gtk_builder_get_object (builder, "button9"), "clicked",
                          G_CALLBACK (solipa_mail_ui_on_btn_attachment_add_clicked), (gpointer)solipa_mail_ui);
-       g_signal_connect (gtk_builder_get_object (builder, "button10"), "clicked",
+       g_signal_connect (priv->btn_attachment_remove, "clicked",
                          G_CALLBACK (solipa_mail_ui_on_btn_attachment_remove_clicked), (gpointer)solipa_mail_ui);
 
        g_signal_connect (gtk_builder_get_object (builder, "button6"), "clicked",
@@ -388,6 +404,9 @@ solipa_mail_ui_send (SolipaMailUI *mailui)
 
        CamelInternetAddress *addr;
 
+       GtkTreeIter iter;
+       gchar *filename;
+
        g_return_val_if_fail (IS_SOLIPA_MAIL_UI (mailui), FALSE);
 
        SolipaMailUIPrivate *priv = SOLIPA_MAIL_UI_GET_PRIVATE (mailui);
@@ -503,6 +522,17 @@ solipa_mail_ui_send (SolipaMailUI *mailui)
 
        solipa_mail_set_body (smail, body, NULL);
 
+       if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->lstore_attachments), &iter))
+               {
+                       do
+                       {
+                               gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_attachments), &iter,
+                                                   COL_ATTACHMENTS_FILE_NAME, &filename,
+                                                   -1);
+                               solipa_mail_add_attachment (smail, filename);
+                       } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->lstore_attachments), &iter));
+               }
+
        if (g_strcmp0 (g_strstrip (smtpuri), "") != 0)
                {
                        solipa_mail_send (smail, smtpuri);
@@ -615,6 +645,10 @@ solipa_mail_ui_set_property (GObject *object,
                                gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->body), g_value_get_boolean (value));
                                break;
 
+                       case PROP_WITH_ATTACHMENTS:
+                               gtk_widget_set_visible (priv->iv_attachments, g_value_get_boolean (value));
+                               break;
+
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;
@@ -701,6 +735,10 @@ solipa_mail_ui_get_property (GObject *object,
                                g_value_set_boolean (value, gtk_text_view_get_editable (GTK_TEXT_VIEW (priv->body)));
                                break;
 
+                       case PROP_WITH_ATTACHMENTS:
+                               g_value_set_boolean (value, gtk_widget_get_visible (priv->iv_attachments));
+                               break;
+
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;
@@ -708,6 +746,28 @@ solipa_mail_ui_get_property (GObject *object,
 }
 
 /* CALLBACK */
+static void
+solipa_mail_ui_on_iv_attachment_selection_changed (GtkIconView *iconview,
+                                  gpointer user_data)
+{
+       GList *items;
+
+       SolipaMailUI *solipa_mail_ui = (SolipaMailUI *)user_data;
+       SolipaMailUIPrivate *priv = SOLIPA_MAIL_UI_GET_PRIVATE (solipa_mail_ui);
+
+       items = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (priv->iv_attachments));
+       if (items == NULL)
+               {
+                       gtk_widget_set_sensitive (priv->btn_attachment_remove, FALSE);
+               }
+       else
+               {
+                       gtk_widget_set_sensitive (priv->btn_attachment_remove, TRUE);
+                       g_list_foreach (items, (GFunc)gtk_tree_path_free, NULL);
+                       g_list_free (items);
+               }
+}
+
 static void
 solipa_mail_ui_on_btn_attachment_add_clicked (GtkButton *button,
                                   gpointer user_data)
@@ -720,6 +780,13 @@ solipa_mail_ui_on_btn_attachment_add_clicked (GtkButton *button,
        gchar *filename;
        GtkTreeIter iter;
 
+       GError *error;
+       GFile *file;
+       GFileInfo *file_info;
+       GIcon *icon;
+       GInputStream *istream;
+       GdkPixbuf *pixbuf;
+
        dialog = gtk_file_chooser_dialog_new ("Choose a file to attach.",
                                              NULL,
                                              GTK_FILE_CHOOSER_ACTION_OPEN,
@@ -729,9 +796,73 @@ solipa_mail_ui_on_btn_attachment_add_clicked (GtkButton *button,
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
                {
                        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+                       if (filename == NULL)
+                               {
+                                       gtk_widget_destroy (dialog);
+                                       return;
+                               }
+
+                       file = g_file_new_for_path (filename);
+                       if (!g_file_query_exists (file, NULL))
+                               {
+                                       gtk_widget_destroy (dialog);
+                                       return;
+                               }
+                       error = NULL;
+                       file_info = g_file_query_info (file,
+                                                      G_FILE_ATTRIBUTE_STANDARD_ICON,
+                                                      0,
+                                                      NULL,
+                                                      &error);
+
+                       pixbuf = NULL;
+                       icon = g_file_info_get_icon (file_info);
+                       if (icon != NULL)
+                               {
+                                       if (G_IS_THEMED_ICON (icon))
+                                               {
+                                                       GtkIconTheme *theme;
+                                                       const gchar * const *names;
+
+                                                       theme = gtk_icon_theme_get_default ();
+                                                       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
+
+                                                       error = NULL;
+                                                       pixbuf = gtk_icon_theme_load_icon (theme,
+                                                                                          names[0],
+                                                                                          64,
+                                                                                          GTK_ICON_LOOKUP_USE_BUILTIN,
+                                                                                          &error);
+                                               }
+                                       else
+                                               {
+                                                       error = NULL;
+                                                       istream = g_loadable_icon_load (G_LOADABLE_ICON (G_FILE_ICON (icon)),
+                                                                                       64,
+                                                                                       NULL, NULL,
+                                                                                       &error);
+                                                       error = NULL;
+                                                       pixbuf = gdk_pixbuf_new_from_stream (istream, NULL, &error);
+                                               }
+                               }
+
+                       if (pixbuf == NULL)
+                               {
+                                       GtkIconSet *icon_set;
+                                       icon_set = gtk_icon_factory_lookup_default ("gtk-file");
+                                       pixbuf = gtk_icon_set_render_icon (icon_set,
+                                                                          gtk_widget_get_style (priv->iv_attachments),
+                                                                          GTK_TEXT_DIR_NONE,
+                                                                          GTK_STATE_NORMAL,
+                                                                          GTK_ICON_SIZE_DND,
+                                                                          priv->iv_attachments, NULL);
+                               }
+
                        gtk_list_store_append (priv->lstore_attachments, &iter);
                        gtk_list_store_set (priv->lstore_attachments, &iter,
                                            COL_ATTACHMENTS_FILE_NAME, filename,
+                                           COL_ATTACHMENTS_FILE_NAME_BASE, g_file_get_basename (file),
+                                           COL_ATTACHMENTS_ICON, pixbuf,
                                            -1);
                        g_free (filename);
                }
@@ -742,8 +873,24 @@ static void
 solipa_mail_ui_on_btn_attachment_remove_clicked (GtkButton *button,
                                   gpointer user_data)
 {
+       GList *items;
+       GtkTreePath *path;
+       GtkTreeIter iter;
+
        SolipaMailUI *solipa_mail_ui = (SolipaMailUI *)user_data;
        SolipaMailUIPrivate *priv = SOLIPA_MAIL_UI_GET_PRIVATE (solipa_mail_ui);
+
+       items = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (priv->iv_attachments));
+       if (items != NULL)
+               {
+                       do
+                               {
+                                       path = (GtkTreePath *)items->data;
+                                       gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->lstore_attachments), &iter, path);
+                                       gtk_list_store_remove (priv->lstore_attachments, &iter);
+                                       items = g_list_next (items);
+                               } while (items != NULL);
+               }
 }
 
 static void