Do ignore empty recipients when checking names
[modest] / src / modest-email-clipboard.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <modest-defs.h>
31 #include <modest-email-clipboard.h>
32 #include <modest-tny-folder.h>
33
34 /* 'private'/'protected' functions */
35 static void modest_email_clipboard_class_init (ModestEmailClipboardClass * klass);
36 static void modest_email_clipboard_init       (ModestEmailClipboard * obj);
37 static void modest_email_clipboard_finalize   (GObject * obj);
38
39 /* globals */
40 static GObjectClass *parent_class = NULL;
41
42 struct _ModestEmailClipboardPrivate {
43         TnyList    *selection;
44         TnyFolder  *src;        
45         gchar      **hidding;
46         gboolean   delete;
47 };
48
49
50 #define MODEST_EMAIL_CLIPBOARD_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51                                                 MODEST_TYPE_EMAIL_CLIPBOARD, \
52                                                 ModestEmailClipboardPrivate))
53
54
55 GType
56 modest_email_clipboard_get_type (void)
57 {
58         static GType my_type = 0;
59
60         if (G_UNLIKELY (!my_type)) {
61                 static const GTypeInfo my_info = {
62                         sizeof (ModestEmailClipboardClass),
63                         NULL,   /* base init */
64                         NULL,   /* base finalize */
65                         (GClassInitFunc) modest_email_clipboard_class_init,
66                         NULL,   /* class finalize */
67                         NULL,   /* class data */
68                         sizeof (ModestEmailClipboard),
69                         1,      /* n_preallocs */
70                         (GInstanceInitFunc) modest_email_clipboard_init,
71                         NULL
72                 };
73
74                 my_type = g_type_register_static (G_TYPE_OBJECT,
75                                                   "ModestEmailClipboard",
76                                                   &my_info, 0);
77         }
78         return my_type;
79 }
80
81
82
83 static void
84 modest_email_clipboard_class_init (ModestEmailClipboardClass * klass)
85 {
86         GObjectClass *gobject_class;
87         gobject_class = (GObjectClass *) klass;
88
89         parent_class = g_type_class_peek_parent (klass);
90         gobject_class->finalize = modest_email_clipboard_finalize;
91
92         g_type_class_add_private (gobject_class,
93                                   sizeof (ModestEmailClipboardPrivate));
94
95         /* signal definitions */
96 }
97
98
99 static void
100 modest_email_clipboard_init (ModestEmailClipboard * obj)
101 {
102
103         if (obj->priv == NULL)
104                 obj->priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (obj);
105         
106         obj->priv->src = NULL;
107         obj->priv->selection = NULL;
108         obj->priv->hidding = NULL;
109         obj->priv->delete = FALSE;
110 }
111
112 static void
113 modest_email_clipboard_finalize (GObject * obj)
114 {
115         /* Clear objects stored on clipboard */
116         modest_email_clipboard_clear (MODEST_EMAIL_CLIPBOARD(obj));             
117
118         G_OBJECT_CLASS(parent_class)->finalize (obj);
119 }
120
121
122 ModestEmailClipboard *
123 modest_email_clipboard_new (void)
124 {
125         GObject *obj;
126         obj = G_OBJECT (g_object_new (MODEST_TYPE_EMAIL_CLIPBOARD, NULL));
127         
128         return MODEST_EMAIL_CLIPBOARD (obj);
129 }
130
131
132 void
133 modest_email_clipboard_get_data (ModestEmailClipboard *self,
134                                  TnyFolder **src_folder,
135                                  TnyList **data,
136                                  gboolean *delete)
137 {
138         ModestEmailClipboardPrivate *priv = NULL;;
139
140         *src_folder = NULL;
141         *data = NULL;
142         *delete = FALSE;
143
144         g_return_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self));
145         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
146         g_return_if_fail (TNY_IS_FOLDER (priv->src));
147
148         /* if no data into clipboard, do nothing */
149         if (modest_email_clipboard_cleared (self)) return;
150                 
151         /* Get data stored on clipboard */
152         *src_folder = g_object_ref(priv->src);
153         if (priv->selection)
154                 *data = g_object_ref(priv->selection);
155         *delete = priv->delete;
156
157         /* Clear objects stored on clipboard */
158         modest_email_clipboard_clear (MODEST_EMAIL_CLIPBOARD(self));            
159 }
160
161 void
162 modest_email_clipboard_set_data (ModestEmailClipboard *self,
163                                  TnyFolder *src_folder, 
164                                  TnyList *data,
165                                  gboolean delete)
166 {
167         ModestEmailClipboardPrivate *priv = NULL;;
168         TnyIterator *iter = NULL;
169         GObject *obj = NULL;                    
170         gchar *id = NULL;
171         guint i, n_selected;
172
173         g_return_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self));
174         g_return_if_fail (TNY_IS_FOLDER (src_folder));
175         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
176
177         /* if data into clipboard, clear them  */
178         if (!modest_email_clipboard_cleared (self)) 
179                 modest_email_clipboard_clear (self);
180         
181         /* set new data */
182         priv->src = g_object_ref (src_folder);
183         if (data != NULL)
184                 priv->selection = g_object_ref (data);
185         priv->delete = delete;
186         priv->hidding = NULL;
187
188         /* Fill hidding array (for cut operation) */    
189         if (delete) {
190                 n_selected = 1;
191                 if (data != NULL) {
192                         n_selected = tny_list_get_length (data);        
193                         priv->hidding = g_malloc0(sizeof(gchar *) * n_selected);        
194                         iter = tny_list_create_iterator (priv->selection);
195                         i = 0;
196                         while (!tny_iterator_is_done (iter)) {
197                                 obj = tny_iterator_get_current (iter);
198                                 if (obj && TNY_IS_HEADER (obj))
199                                         id = tny_header_dup_message_id (TNY_HEADER (obj));
200                                 
201                                 priv->hidding[i++] = id;
202                                 tny_iterator_next (iter);
203                                 
204                                 if (obj)
205                                         g_object_unref (obj);
206                         }
207                         g_object_unref (iter);
208                 }
209                 else {
210                         priv->hidding = g_malloc0(sizeof(gchar *));     
211                         id = g_strdup (tny_folder_get_id (src_folder));
212                         priv->hidding[0] = id;                  
213                 }
214         }
215 }
216
217 void
218 modest_email_clipboard_clear (ModestEmailClipboard *self)
219 {
220         ModestEmailClipboardPrivate *priv = NULL;
221         guint i, n_selected;
222
223         g_return_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self));
224         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
225
226         n_selected = 1;
227         if (priv->src) 
228                 g_object_unref (priv->src);
229         if (priv->selection) {
230                 n_selected = tny_list_get_length(priv->selection);
231                 g_object_unref (priv->selection);
232         }
233         if (priv->hidding) {            
234                 for (i=0; i < n_selected; i++) 
235                         g_free (priv->hidding[i]);
236                 g_free(priv->hidding);
237         }
238
239         priv->src = NULL;
240         priv->selection = NULL;
241         priv->hidding = NULL;
242         priv->delete = FALSE;
243 }
244
245 gboolean
246 modest_email_clipboard_cleared (ModestEmailClipboard *self)
247 {
248         gboolean cleared = FALSE;
249
250         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), TRUE);
251
252         cleared = ((self->priv->src == NULL) && (self->priv->selection == NULL));
253
254         return cleared;
255 }
256
257 gboolean 
258 modest_email_clipboard_check_source_folder (ModestEmailClipboard *self,
259                                             TnyFolder *folder)
260 {
261         TnyFolderType folder_type1;
262         TnyFolderType folder_type2;
263         ModestEmailClipboardPrivate *priv = NULL;;
264         const gchar *id1 = NULL;
265         const gchar *id2 = NULL;
266         gboolean same_folder = FALSE;
267
268         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), TRUE);
269         g_return_val_if_fail (TNY_IS_FOLDER (folder), TRUE);
270         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
271         
272         /* If cleared, return always FALSE*/
273         if (modest_email_clipboard_cleared (self)) return FALSE;
274
275         /* Check target and source folders */
276         id1 = tny_folder_get_id (priv->src);
277         id2 = tny_folder_get_id (TNY_FOLDER(folder));   
278         folder_type1 = modest_tny_folder_guess_folder_type (priv->src);
279         folder_type2 = modest_tny_folder_guess_folder_type (folder);
280         
281         if (folder_type1 == TNY_FOLDER_TYPE_INVALID || folder_type2 == TNY_FOLDER_TYPE_INVALID)
282                 g_warning ("%s: BUG: folder of type TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
283         
284         same_folder = ((folder_type1 == folder_type2) && 
285                        (!g_ascii_strcasecmp (id1, id2)));
286         
287         return same_folder;
288 }
289
290 gboolean 
291 modest_email_clipboard_headers_copied (ModestEmailClipboard *self)
292 {
293         ModestEmailClipboardPrivate *priv = NULL;;
294         gboolean result = FALSE;
295
296         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), FALSE);
297         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
298
299         result = priv->selection != NULL;
300
301         return result;
302 }
303
304 gboolean 
305 modest_email_clipboard_folder_copied (ModestEmailClipboard *self)
306 {
307         ModestEmailClipboardPrivate *priv = NULL;;
308         gboolean result = FALSE;
309
310         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), FALSE);
311         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
312         
313         result = ((priv->selection == NULL) && (priv->src != NULL));
314         
315         return result; 
316 }
317
318 const gchar *
319 modest_email_clipboard_get_folder_name (ModestEmailClipboard *self)
320 {
321         ModestEmailClipboardPrivate *priv = NULL;;
322         const gchar *folder_name = NULL;
323
324         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), NULL);
325         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
326         
327         /* If cleared, return always FALSE*/
328         if (modest_email_clipboard_cleared (self)) return NULL;
329
330         /* Check target and source folders */
331         folder_name = tny_folder_get_name (priv->src);
332         
333         return folder_name;
334 }
335
336 const gchar **
337 modest_email_clipboard_get_hidding_ids (ModestEmailClipboard *self,
338                                         guint *n_selected)
339 {
340         ModestEmailClipboardPrivate *priv = NULL;;
341
342         *n_selected = 0;
343
344         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), NULL);
345         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
346
347         if (priv->selection != NULL)
348                 *n_selected = tny_list_get_length (priv->selection);
349         else
350                 *n_selected = 1;
351
352         return (const gchar **) priv->hidding;
353 }
354