* check for a valid foldername
[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-email-clipboard.h>
31
32 /* 'private'/'protected' functions */
33 static void modest_email_clipboard_class_init (ModestEmailClipboardClass * klass);
34 static void modest_email_clipboard_init       (ModestEmailClipboard * obj);
35 static void modest_email_clipboard_finalize   (GObject * obj);
36
37 /* globals */
38 static GObjectClass *parent_class = NULL;
39
40 typedef struct _ModestEmailClipboardPrivate ModestEmailClipboardPrivate;
41 struct _ModestEmailClipboardPrivate {
42         TnyList    *selection;
43         TnyFolder  *src;        
44         gchar      **hidding;
45         gboolean   delete;
46 };
47
48
49 #define MODEST_EMAIL_CLIPBOARD_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
50                                                 MODEST_TYPE_EMAIL_CLIPBOARD, \
51                                                 ModestEmailClipboardPrivate))
52
53
54 GType
55 modest_email_clipboard_get_type (void)
56 {
57         static GType my_type = 0;
58
59         if (!my_type) {
60                 static const GTypeInfo my_info = {
61                         sizeof (ModestEmailClipboardClass),
62                         NULL,   /* base init */
63                         NULL,   /* base finalize */
64                         (GClassInitFunc) modest_email_clipboard_class_init,
65                         NULL,   /* class finalize */
66                         NULL,   /* class data */
67                         sizeof (ModestEmailClipboard),
68                         1,      /* n_preallocs */
69                         (GInstanceInitFunc) modest_email_clipboard_init,
70                         NULL
71                 };
72
73                 my_type = g_type_register_static (G_TYPE_OBJECT,
74                                                   "ModestEmailClipboard",
75                                                   &my_info, 0);
76         }
77         return my_type;
78 }
79
80
81
82 static void
83 modest_email_clipboard_class_init (ModestEmailClipboardClass * klass)
84 {
85         GObjectClass *gobject_class;
86         gobject_class = (GObjectClass *) klass;
87
88         parent_class = g_type_class_peek_parent (klass);
89         gobject_class->finalize = modest_email_clipboard_finalize;
90
91         g_type_class_add_private (gobject_class,
92                                   sizeof (ModestEmailClipboardPrivate));
93
94         /* signal definitions */
95 }
96
97
98 static void
99 modest_email_clipboard_init (ModestEmailClipboard * obj)
100 {
101         ModestEmailClipboardPrivate *priv =
102                 MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (obj);
103         
104         priv->src = NULL;
105         priv->selection = NULL;
106         priv->hidding = NULL;
107         priv->delete = FALSE;
108 }
109
110 static void
111 modest_email_clipboard_finalize (GObject * obj)
112 {
113         /* Clear objects stored on clipboard */
114         modest_email_clipboard_clear (MODEST_EMAIL_CLIPBOARD(obj));             
115
116         G_OBJECT_CLASS(parent_class)->finalize (obj);
117 }
118
119
120 ModestEmailClipboard *
121 modest_email_clipboard_new ()
122 {
123         GObject *obj;
124         ModestEmailClipboardPrivate *priv;
125
126         obj = G_OBJECT (g_object_new (MODEST_TYPE_EMAIL_CLIPBOARD, NULL));
127         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (obj);
128         
129         return MODEST_EMAIL_CLIPBOARD (obj);
130 }
131
132
133 void
134 modest_email_clipboard_get_data (ModestEmailClipboard *self,
135                                  TnyFolder **src_folder,
136                                  TnyList **data,
137                                  gboolean *delete)
138 {
139         ModestEmailClipboardPrivate *priv = NULL;;
140
141         *src_folder = NULL;
142         *data = NULL;
143         *delete = FALSE;
144
145         g_return_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self));
146         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
147         g_return_if_fail (TNY_IS_FOLDER (priv->src));
148
149         /* if no data into clipboard, do nothing */
150         if (modest_email_clipboard_cleared (self)) return;
151                 
152         /* Get data stored on clipboard */
153         *src_folder = g_object_ref(priv->src);
154         if (priv->selection)
155                 *data = g_object_ref(priv->selection);
156         *delete = priv->delete;
157
158         /* Clear objects stored on clipboard */
159         modest_email_clipboard_clear (MODEST_EMAIL_CLIPBOARD(self));            
160 }
161
162 void
163 modest_email_clipboard_set_data (ModestEmailClipboard *self,
164                                  TnyFolder *src_folder, 
165                                  TnyList *data,
166                                  gboolean delete)
167 {
168         ModestEmailClipboardPrivate *priv = NULL;;
169         TnyIterator *iter = NULL;
170         GObject *obj = NULL;                    
171         gchar *id = NULL;
172         guint i, n_selected;
173
174         g_return_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self));
175         g_return_if_fail (TNY_IS_FOLDER (src_folder));
176         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
177
178         /* if data into clipboard, clear them  */
179         if (!modest_email_clipboard_cleared (self)) 
180                 modest_email_clipboard_clear (self);
181         
182         /* set new data */
183         priv->src = g_object_ref (src_folder);
184         if (data != NULL)
185                 priv->selection = g_object_ref (data);
186         priv->delete = delete;
187         priv->hidding = NULL;
188
189         /* Fill hidding array (for cut operation) */    
190         if (delete) {
191                 n_selected = 1;
192                 if (data != NULL) {
193                         n_selected = tny_list_get_length (data);        
194                         priv->hidding = g_malloc0(sizeof(gchar *) * n_selected);        
195                         iter = tny_list_create_iterator (priv->selection);
196                         i = 0;
197                         while (!tny_iterator_is_done (iter)) {
198                                 obj = tny_iterator_get_current (iter);
199                                 if (obj && TNY_IS_HEADER (obj))
200                                         id = g_strdup(tny_header_get_message_id (TNY_HEADER (obj)));
201                                 
202                                 priv->hidding[i++] = id;
203                                 tny_iterator_next (iter);
204                                 
205                                 if (obj)
206                                         g_object_unref (obj);
207                         }
208                         g_object_unref (iter);
209                 }
210                 else {
211                         priv->hidding = g_malloc0(sizeof(gchar *));     
212                         id = g_strdup (tny_folder_get_id (src_folder));
213                         priv->hidding[0] = id;                  
214                 }
215         }
216 }
217
218 void
219 modest_email_clipboard_clear (ModestEmailClipboard *self)
220 {
221         ModestEmailClipboardPrivate *priv = NULL;
222         guint i, n_selected;
223
224         g_return_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self));
225         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
226
227         n_selected = 1;
228         if (priv->src) 
229                 g_object_unref (priv->src);
230         if (priv->selection) {
231                 n_selected = tny_list_get_length(priv->selection);
232                 g_object_unref (priv->selection);
233         }
234         if (priv->hidding) {            
235                 for (i=0; i < n_selected; i++) 
236                         g_free (priv->hidding[i]);
237                 g_free(priv->hidding);
238         }
239
240         priv->src = NULL;
241         priv->selection = NULL;
242         priv->hidding = NULL;
243         priv->delete = FALSE;
244 }
245
246 gboolean
247 modest_email_clipboard_cleared (ModestEmailClipboard *self)
248 {
249         ModestEmailClipboardPrivate *priv = NULL;;
250         gboolean cleared = FALSE;
251
252         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), TRUE);
253         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
254
255         cleared = ((priv->src == NULL) && (priv->selection == NULL));
256
257         return cleared;
258 }
259
260 gboolean 
261 modest_email_clipboard_check_source_folder (ModestEmailClipboard *self,
262                                             const TnyFolder *folder)
263 {
264         ModestEmailClipboardPrivate *priv = NULL;;
265         const gchar *id1 = NULL;
266         const gchar *id2 = NULL;
267         gboolean same_folder = FALSE;
268
269         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), TRUE);
270         g_return_val_if_fail (TNY_IS_FOLDER (folder), TRUE);
271         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
272         
273         /* If cleared, return always FALSE*/
274         if (modest_email_clipboard_cleared (self)) return FALSE;
275
276         /* Check target and source folders */
277         id1 = tny_folder_get_id (priv->src);
278         id2 = tny_folder_get_id (TNY_FOLDER(folder));
279         same_folder = !g_ascii_strcasecmp (id1, id2);
280         
281         return same_folder;
282 }
283
284 const gchar **
285 modest_email_clipboard_get_hidding_ids (ModestEmailClipboard *self,
286                                         guint *n_selected)
287 {
288         ModestEmailClipboardPrivate *priv = NULL;;
289
290         *n_selected = 0;
291
292         g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (self), NULL);
293         priv = MODEST_EMAIL_CLIPBOARD_GET_PRIVATE (self);
294
295         if (priv->selection != NULL)
296                 *n_selected = tny_list_get_length (priv->selection);
297         else
298                 *n_selected = 1;
299
300         return (const gchar **) priv->hidding;
301 }
302