Work to improve drafts folder management (fixes NB#56483).
[modest] / src / modest-mail-operation.h
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 #ifndef __MODEST_MAIL_OPERATION_H__
31 #define __MODEST_MAIL_OPERATION_H__
32
33 #include <tny-transport-account.h>
34 #include <tny-folder-store.h>
35
36 G_BEGIN_DECLS
37
38 /* convenience macros */
39 #define MODEST_TYPE_MAIL_OPERATION             (modest_mail_operation_get_type())
40 #define MODEST_MAIL_OPERATION(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),MODEST_TYPE_MAIL_OPERATION,ModestMailOperation))
41 #define MODEST_MAIL_OPERATION_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),MODEST_TYPE_MAIL_OPERATION,GObject))
42 #define MODEST_IS_MAIL_OPERATION(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),MODEST_TYPE_MAIL_OPERATION))
43 #define MODEST_IS_MAIL_OPERATION_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),MODEST_TYPE_MAIL_OPERATION))
44 #define MODEST_MAIL_OPERATION_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj),MODEST_TYPE_MAIL_OPERATION,ModestMailOperationClass))
45
46 typedef struct _ModestMailOperation      ModestMailOperation;
47 typedef struct _ModestMailOperationClass ModestMailOperationClass;
48
49 /**
50  * ModestMailOperationStatus:
51  *
52  * The state of a mail operation
53  */
54 typedef enum _ModestMailOperationStatus {
55         MODEST_MAIL_OPERATION_STATUS_INVALID,
56         MODEST_MAIL_OPERATION_STATUS_SUCCESS,
57         MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS,
58         MODEST_MAIL_OPERATION_STATUS_FAILED,
59         MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS,
60         MODEST_MAIL_OPERATION_STATUS_CANCELED
61 } ModestMailOperationStatus;
62
63 /**
64  * ModestMailOperationId:
65  *
66  * The id for identifying the type of mail operation
67  */
68 typedef enum {
69         MODEST_MAIL_OPERATION_TYPE_SEND,
70         MODEST_MAIL_OPERATION_TYPE_RECEIVE,
71         MODEST_MAIL_OPERATION_TYPE_OPEN,
72         MODEST_MAIL_OPERATION_TYPE_DELETE,
73         MODEST_MAIL_OPERATION_TYPE_INFO,
74         MODEST_MAIL_OPERATION_TYPE_UNKNOWN,
75 } ModestMailOperationTypeOperation;
76
77 /**
78  * ErrorCheckingAsyncUserCallback:
79  *
80  * @mail_op: the current mail operation.
81  * @user_data: generic data passed to user defined function.
82  *
83  * This function implements required actions to performs under error
84  * states.  
85  */
86 typedef void (*ErrorCheckingUserCallback) (ModestMailOperation *mail_op, gpointer user_data);
87
88 /**
89  * GetMsgAsyncUserCallback:
90  *
91  * @obj: a #GObject generic object which has created current mail operation.
92  * @msg: a #TnyMsg message retrieved by async operation.
93  * @user_data: generic data passed to user defined function.
94  *
95  * This function will be called after get_msg_cb private function, which is
96  * used as tinymail operation callback. The private function fills private 
97  * fields of mail operation and calls user defined callback if it exists.
98  */
99 typedef void (*GetMsgAsyncUserCallback) (ModestMailOperation *mail_op, 
100                                          TnyHeader *header, 
101                                          TnyMsg *msg, 
102                                          gpointer user_data);
103
104 /**
105  * XferMsgAsynUserCallback:
106  *
107  * @obj: a #GObject generic object which has created current mail operation.
108  * @user_data: generic data passed to user defined function.
109  *
110  * This function will be called after transfer_msgs_cb private function, which is
111  * used as tinymail operation callback. The private function fills private 
112  * fields of mail operation and calls user defined callback if it exists.
113  */
114 typedef void (*XferMsgsAsynUserCallback) (const GObject *obj, gpointer user_data);
115
116
117 /* This struct represents the internal state of a mail operation in a
118    given time */
119 typedef struct {
120         guint      done;
121         guint      total;
122         gboolean   finished;
123         ModestMailOperationStatus        status;
124         ModestMailOperationTypeOperation op_type;
125 } ModestMailOperationState;
126
127
128 struct _ModestMailOperation {
129          GObject parent;
130         /* insert public members, if any */
131 };
132
133 struct _ModestMailOperationClass {
134         GObjectClass parent_class;
135
136         /* Signals */
137         void (*progress_changed) (ModestMailOperation *self, ModestMailOperationState *state, gpointer user_data);
138 };
139
140 /* member functions */
141 GType        modest_mail_operation_get_type    (void) G_GNUC_CONST;
142
143 /**
144  * modest_mail_operation_new:
145  * @id: a #ModestMailOperationId identification of operation type.
146  * @source: a #GObject which creates this new operation.
147  * 
148  * Creates a new instance of class #ModestMailOperation, using parameters
149  * to initialize its private structure. @source parameter may be NULL.
150  **/
151 ModestMailOperation*    modest_mail_operation_new     (ModestMailOperationTypeOperation type,
152                                                        GObject *source);
153
154 /**
155  * modest_mail_operation_new_with_error_handling:
156  * @id: a #ModestMailOperationId identification of operation type.
157  * @source: a #GObject which creates this new operation.
158  * @error_handler: a #ErrorCheckingUserCallback function to performs operations when 
159  * an error occurs.
160  * 
161  * Creates a new instance of class #ModestMailOperation, using parameters
162  * to initialize its private structure. @source parameter may be NULL. 
163  * @error_handler can not be NULL, but it will be returned an mail operation
164  * object without error handling capability.
165  **/
166 ModestMailOperation*    modest_mail_operation_new_with_error_handling     (ModestMailOperationTypeOperation op_type,
167                                                                            GObject *source,
168                                                                            ErrorCheckingUserCallback error_handler,
169                                                                            gpointer user_data);
170 /**
171  * modest_mail_operation_execute_error_handler
172  * @self: a #ModestMailOperation
173  * 
174  * Executes error handler if exists. The error handler is the one that
175  * MUST free the user data passed to the
176  * modest_mail_operation_new_with_error_handling constructor
177  **/
178 void
179 modest_mail_operation_execute_error_handler (ModestMailOperation *self);
180
181 /**
182  * modest_mail_operation_get_type_operation
183  * @self: a #ModestMailOperation
184  * 
185  * Gets the private op_type field of mail operation. This op_type
186  * identifies the class/type of mail operation.
187  **/
188 ModestMailOperationTypeOperation
189 modest_mail_operation_get_type_operation (ModestMailOperation *self);
190
191 /**
192  * modest_mail_operation_is_mine
193  * @self: a #ModestMailOperation
194  * @source: a #GObject to check if it have created @self operation.
195  * 
196  * Check if @source object its owner of @self mail operation.
197  *
198  * returns: TRUE if source its owner, FALSE otherwise.
199  **/
200 gboolean 
201 modest_mail_operation_is_mine (ModestMailOperation *self, 
202                                GObject *me);
203
204 /**
205  * modest_mail_operation_get_source
206  * @self: a #ModestMailOperation
207  *
208  * returns a new reference to the object that created the mail
209  * operation passed to the constructor, or NULL if none. The caller
210  * must free the new reference
211  **/
212 GObject *
213 modest_mail_operation_get_source (ModestMailOperation *self);
214
215 /* fill in other public functions, eg.: */
216
217 /**
218  * modest_mail_operation_send_mail:
219  * @self: a #ModestMailOperation
220  * @transport_account: a non-NULL #TnyTransportAccount
221  * @msg: a non-NULL #TnyMsg
222  * 
223  * Sends and already existing message using the provided
224  * #TnyTransportAccount. This operation is synchronous, so the
225  * #ModestMailOperation should not be added to any
226  * #ModestMailOperationQueue
227   **/
228 void    modest_mail_operation_send_mail       (ModestMailOperation *self,
229                                                TnyTransportAccount *transport_account,
230                                                TnyMsg* msg);
231
232 /**
233  * modest_mail_operation_send_new_mail:
234  * @self: a #ModestMailOperation
235  * @transport_account: a non-NULL #TnyTransportAccount
236  * @draft_msg: a #TnyMsg of the origin draft message, if any
237  * @from: the email address of the mail sender
238  * @to: a non-NULL email address of the mail receiver
239  * @cc: a comma-separated list of email addresses where to send a carbon copy
240  * @bcc: a comma-separated list of email addresses where to send a blind carbon copy
241  * @subject: the subject of the new mail
242  * @plain_body: the plain text body of the new mail.
243  * @html_body: the html version of the body of the new mail. If NULL, the mail will
244  *             be sent with the plain body only.
245  * @attachments_list: a #GList of attachments, each attachment must be a #TnyMimePart
246  * 
247  * Sends a new mail message using the provided
248  * #TnyTransportAccount. This operation is synchronous, so the
249  * #ModestMailOperation should not be added to any
250  * #ModestMailOperationQueue
251   **/
252 void    modest_mail_operation_send_new_mail   (ModestMailOperation *self,
253                                                TnyTransportAccount *transport_account,
254                                                TnyMsg *draft_msg,
255                                                const gchar *from,
256                                                const gchar *to,
257                                                const gchar *cc,
258                                                const gchar *bcc,
259                                                const gchar *subject,
260                                                const gchar *plain_body,
261                                                const gchar *html_body,
262                                                const GList *attachments_list,
263                                                TnyHeaderFlags priority_flags);
264
265
266 /**
267  * modest_mail_operation_save_to_drafts:
268  * @self: a #ModestMailOperation
269  * @transport_account: a non-NULL #TnyTransportAccount
270  * @draft_msg: the previous draft message, in case it's an update
271  * to an existing draft.
272  * @from: the email address of the mail sender
273  * @to: a non-NULL email address of the mail receiver
274  * @cc: a comma-separated list of email addresses where to send a carbon copy
275  * @bcc: a comma-separated list of email addresses where to send a blind carbon copy
276  * @subject: the subject of the new mail
277  * @plain_body: the plain text body of the new mail.
278  * @html_body: the html version of the body of the new mail. If NULL, the mail will
279  *             be sent with the plain body only.
280  * @attachments_list: a #GList of attachments, each attachment must be a #TnyMimePart
281  * 
282  * Save a mail message to drafts using the provided
283  * #TnyTransportAccount. This operation is synchronous, so the
284  * #ModestMailOperation should not be added to any
285  * #ModestMailOperationQueue
286   **/
287 void    modest_mail_operation_save_to_drafts   (ModestMailOperation *self,
288                                                 TnyTransportAccount *transport_account,
289                                                 TnyMsg *draft_msg,
290                                                 const gchar *from,
291                                                 const gchar *to,
292                                                 const gchar *cc,
293                                                 const gchar *bcc,
294                                                 const gchar *subject,
295                                                 const gchar *plain_body,
296                                                 const gchar *html_body,
297                                                 const GList *attachments_list,
298                                                 TnyHeaderFlags priority_flags);
299 /**
300  * modest_mail_operation_update_account:
301  * @self: a #ModestMailOperation
302  * @account_name: the id of a Modest account
303  * 
304  * Asynchronously refreshes the root folders of the given store
305  * account. The caller should add the #ModestMailOperation to a
306  * #ModestMailOperationQueue and then free it. The caller will be
307  * notified by the "progress_changed" signal each time the progress of
308  * the operation changes.
309  *
310  * Example
311  * <informalexample><programlisting>
312  * queue = modest_tny_platform_factory_get_modest_mail_operation_queue_instance (fact)
313  * mail_op = modest_mail_operation_new ();
314  * g_signal_connect (G_OBJECT (mail_op), "progress_changed", G_CALLBACK(on_progress_changed), NULL);
315  * modest_mail_operation_queue_add (queue, mail_op);
316  * modest_mail_operation_update_account (mail_op, account_name)
317  * g_object_unref (G_OBJECT (mail_op));
318  * </programlisting></informalexample>
319  *
320  * Note that the account_name *MUST* be a modest account name, not a
321  * tinymail store account name
322  * 
323  * Returns: TRUE if the mail operation could be started, or FALSE otherwise
324  **/
325 gboolean      modest_mail_operation_update_account (ModestMailOperation *self,
326                                                     const gchar *account_name);
327
328 /* Functions that perform store operations */
329
330 /**
331  * modest_mail_operation_create_folder:
332  * @self: a #ModestMailOperation
333  * @parent: the #TnyFolderStore which is the parent of the new folder
334  * @name: the non-NULL new name for the new folder
335  * 
336  * Creates a new folder as a children of a existing one. If the store
337  * account supports subscriptions this method also sets the new folder
338  * as subscribed. This operation is synchronous, so the
339  * #ModestMailOperation should not be added to any
340  * #ModestMailOperationQueue
341  * 
342  * Returns: a newly created #TnyFolder or NULL in case of error.
343  **/
344 TnyFolder*    modest_mail_operation_create_folder  (ModestMailOperation *self,
345                                                     TnyFolderStore *parent,
346                                                     const gchar *name);
347
348 /**
349  * modest_mail_operation_remove_folder:
350  * @self: a #ModestMailOperation
351  * @folder: a #TnyFolder
352  * @remove_to_trash: TRUE to move it to trash or FALSE to delete
353  * permanently
354  * 
355  * Removes a folder. This operation is synchronous, so the
356  * #ModestMailOperation should not be added to any
357  * #ModestMailOperationQueue
358  **/
359 void          modest_mail_operation_remove_folder  (ModestMailOperation *self,
360                                                     TnyFolder *folder,
361                                                     gboolean remove_to_trash);
362
363 /**
364  * modest_mail_operation_rename_folder:
365  * @self: a #ModestMailOperation
366  * @folder: a #TnyFolder
367  * @name: a non-NULL name without "/"
368  * 
369  * Renames a given folder with the provided new name. This operation
370  * is synchronous, so the #ModestMailOperation should not be added to
371  * any #ModestMailOperationQueue
372  **/
373 void          modest_mail_operation_rename_folder  (ModestMailOperation *self,
374                                                     TnyFolder *folder, 
375                                                     const gchar *name);
376
377 /**
378  * modest_mail_operation_xfer_folder:
379  * @self: a #ModestMailOperation
380  * @folder: a #TnyFolder
381  * @parent: the new parent of the folder as #TnyFolderStore
382  * @delete_original: wheter or not delete the original folder
383  * 
384  * Sets the given @folder as child of a provided #TnyFolderStore. This
385  * operation also transfers all the messages contained in the folder
386  * and all of his children folders with their messages as well. This
387  * operation is synchronous, so the #ModestMailOperation should not be
388  * added to any #ModestMailOperationQueue.
389  *
390  * If @delete_original is TRUE this function moves the original
391  * folder, if it is FALSE the it just copies it
392  *
393  **/
394 void          modest_mail_operation_xfer_folder    (ModestMailOperation *self,
395                                                     TnyFolder *folder,
396                                                     TnyFolderStore *parent,
397                                                     gboolean delete_original);
398
399 /* Functions that performs msg operations */
400
401 /**
402  * modest_mail_operation_xfer_msgs:
403  * @self: a #ModestMailOperation
404  * @header_list: a #TnyList of #TnyHeader to transfer
405  * @folder: the #TnyFolder where the messages will be transferred
406  * @delete_original: whether or not delete the source messages
407  * @user_callback: a #XferMsgsAsynUserCallback function to call after tinymail callback execution.
408  * @user_data: generic user data which will be passed to @user_callback function.
409  * 
410  * Asynchronously transfers messages from their current folder to
411  * another one. The caller should add the #ModestMailOperation to a
412  * #ModestMailOperationQueue and then free it. The caller will be
413  * notified by the "progress_changed" when the operation is completed.
414  *
415  * If the @delete_original paramter is TRUE then this function moves
416  * the messages between folders, otherwise it copies them.
417  * 
418  * Example
419  * <informalexample><programlisting>
420  * queue = modest_tny_platform_factory_get_modest_mail_operation_queue_instance (fact);
421  * mail_op = modest_mail_operation_new ();
422  * modest_mail_operation_queue_add (queue, mail_op);
423  * g_signal_connect (G_OBJECT (mail_op), "progress_changed", G_CALLBACK(on_progress_changed), queue);
424  *
425  * modest_mail_operation_xfer_msg (mail_op, headers, folder, TRUE);
426  * 
427  * g_object_unref (G_OBJECT (mail_op));
428  * </programlisting></informalexample>
429  *
430  **/
431 void          modest_mail_operation_xfer_msgs      (ModestMailOperation *self,
432                                                     TnyList *header_list, 
433                                                     TnyFolder *folder,
434                                                     gboolean delete_original,
435                                                     XferMsgsAsynUserCallback user_callback,
436                                                     gpointer user_data);
437
438 /**
439  * modest_mail_operation_remove_msg:
440  * @self: a #ModestMailOperation
441  * @header: the #TnyHeader of the message to move
442  * @remove_to_trash: TRUE to move it to trash or FALSE to delete it
443  * permanently
444  * 
445  * Deletes a message. This operation is synchronous, so the
446  * #ModestMailOperation should not be added to any
447  * #ModestMailOperationQueue
448  **/
449 void          modest_mail_operation_remove_msg     (ModestMailOperation *self,
450                                                     TnyHeader *header,
451                                                     gboolean remove_to_trash);
452
453 /**
454  * modest_mail_operation_get_msg:
455  * @self: a #ModestMailOperation
456  * @header_list: the #TnyHeader of the message to get
457  * @user_callback: a #GetMsgAsyncUserCallback function to call after tinymail callback execution.
458  * @user_data: generic user data which will be passed to @user_callback function.
459  * 
460  * Gets a message from header using an user defined @callback function
461  * pased as argument. This operation is asynchronous, so the
462  * #ModestMailOperation should be added to #ModestMailOperationQueue
463  **/
464 void          modest_mail_operation_get_msg     (ModestMailOperation *self,
465                                                  TnyHeader *header, 
466                                                  GetMsgAsyncUserCallback user_callback,
467                                                  gpointer user_data);
468 /**
469  * modest_mail_operation_get_msgs_full:
470  * @self: a #ModestMailOperation
471  * @header_list: a #TnyList of #TnyHeader objects to get and process
472  * @user_callback: a #TnyGetMsgCallback function to call after tinymail operation execution.
473  * @user_data: user data passed to both, user_callback and update_status_callback.
474  * 
475  * Gets messages from headers list and process hem using @callback function
476  * pased as argument. This operation is asynchronous, so the
477  * #ModestMailOperation should be added to #ModestMailOperationQueue
478  **/
479 void          modest_mail_operation_get_msgs_full   (ModestMailOperation *self,
480                                                      TnyList *headers_list,
481                                                      GetMsgAsyncUserCallback user_callback,
482                                                      gpointer user_data,
483                                                      GDestroyNotify notify);
484
485 /* Functions to control mail operations */
486 /**
487  * modest_mail_operation_get_task_done:
488  * @self: a #ModestMailOperation
489  * 
490  * Gets the amount of work done for a given mail operation. This
491  * amount of work is an absolute value.
492  * 
493  * Returns: the amount of work completed
494  **/
495 guint     modest_mail_operation_get_task_done      (ModestMailOperation *self);
496
497 /**
498  * modest_mail_operation_get_task_total:
499  * @self: a #ModestMailOperation
500  * 
501  * Gets the total amount of work that must be done to complete a given
502  * mail operation. This amount of work is an absolute value.
503  * 
504  * Returns: the total required amount of work
505  **/
506 guint     modest_mail_operation_get_task_total     (ModestMailOperation *self);
507
508
509 /**
510  * modest_mail_operation_is_finished:
511  * @self: a #ModestMailOperation
512  * 
513  * Checks if the operation is finished. A #ModestMailOperation is
514  * finished if it's in any of the following states:
515  * MODEST_MAIL_OPERATION_STATUS_SUCCESS,
516  * MODEST_MAIL_OPERATION_STATUS_FAILED,
517  * MODEST_MAIL_OPERATION_STATUS_CANCELED or
518  * MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS
519  * 
520  * Returns: TRUE if the operation is finished, FALSE otherwise
521  **/
522 gboolean  modest_mail_operation_is_finished        (ModestMailOperation *self);
523
524 /**
525  * modest_mail_operation_is_finished:
526  * @self: a #ModestMailOperation
527  * 
528  * Gets the current status of the given mail operation
529  *
530  * Returns: the current status or MODEST_MAIL_OPERATION_STATUS_INVALID in case of error
531  **/
532 ModestMailOperationStatus modest_mail_operation_get_status  (ModestMailOperation *self);
533
534 /**
535  * modest_mail_operation_get_error:
536  * @self: a #ModestMailOperation
537  * 
538  * Gets the error associated to the mail operation if exists
539  * 
540  * Returns: the #GError associated to the #ModestMailOperation if it
541  * exists or NULL otherwise
542  **/
543 const GError*             modest_mail_operation_get_error   (ModestMailOperation *self);
544
545 /**
546  * modest_mail_operation_cancel:
547  * @self: a #ModestMailOperation
548  *
549  * Cancels an active mail operation
550  * 
551  * Returns: TRUE if the operation was succesfully canceled, FALSE otherwise
552  **/
553 gboolean  modest_mail_operation_cancel          (ModestMailOperation *self);
554
555 /**
556  * modest_mail_operation_refresh_folder
557  * @self: a #ModestMailOperation
558  * @folder: the #TnyFolder to refresh
559  * 
560  * Refreshes the contents of a folder
561  */
562 void      modest_mail_operation_refresh_folder  (ModestMailOperation *self,
563                                                  TnyFolder *folder);
564
565 guint     modest_mail_operation_get_id          (ModestMailOperation *self);
566
567 guint     modest_mail_operation_set_id          (ModestMailOperation *self,
568                                                  guint id);
569
570 G_END_DECLS
571
572 #endif /* __MODEST_MAIL_OPERATION_H__ */
573