File: mn-message.gob

package info (click to toggle)
mail-notification 5.4.dfsg.1-14
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,468 kB
  • ctags: 9,615
  • sloc: ansic: 13,970; sh: 2,770; xml: 2,113; makefile: 57
file content (615 lines) | stat: -rw-r--r-- 15,144 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
 * Mail Notification
 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

%headertop{
#include <time.h>
#include "mn-mailbox.h"
%}

%h{
typedef enum
{
  MN_MESSAGE_NEW	= 1 << 0	/* unseen message */
} MNMessageFlags;

typedef struct _MNMessageAction MNMessageAction;

typedef void (*MNMessageActionResultCallback) (MNMessageAction *action, GError *err, gpointer data);

typedef struct
{
  MNMessageAction		*action;
  MNMessage			*message;
  MNMessageActionResultCallback	callback;
  gpointer			data;
} MNMessageActionRequest;

struct _MNMessageAction
{
  const char	*name;
  const char	*icon;
  const char	*label;
  const char	*error_message;

  gboolean (*can_perform) (MNMessage *message);
  void (*perform) (MNMessage *message, MNMessageActionRequest *request);
  void (*done) (MNMessage *message, GError *err);
};

#define MN_MESSAGE_ACTION_ERROR		(mn_message_action_error_quark())

typedef enum
{
  MN_MESSAGE_ACTION_ERROR_OTHER,
  MN_MESSAGE_ACTION_ERROR_CANCELLED
} MNMessageActionError;
%}

%privateheader{
#include "mn-xml.h"

typedef enum
{
  /* include in the XML summary and allow as a command format */
  MN_MESSAGE_PARAM_EXPORT	= MN_XML_PARAM_EXPORT,
} MNMessageParamFlags;

typedef GError *(*MNMessageActionPerformCallback) (MNMessage *message, gpointer data);
%}

%{
#include <errno.h>
#include <glib/gi18n.h>
#include <libgnome/libgnome.h>
#include <libgnomevfs/gnome-vfs.h>
#include "mn-conf.h"
#include "mn-util.h"

typedef struct
{
  MNMessageActionRequest		*request;
  MNMessageActionPerformCallback	callback;
  gpointer				user_data;
} PerformInfo;
%}

%afterdecls{
static const MNMessageAction message_actions[] = {
  {
    "open",
    "mail-open",
    /* translators: header capitalization */
    N_("Open"),
    N_("Unable to open message"),
    self_builtin_can_open,
    self_builtin_open,
    self_open_done
  },
  {
    "mark-as-read",
    "mark",
    /* translators: header capitalization */
    N_("Mark as Read"),
    N_("Unable to mark message as read"),
    self_builtin_can_mark_as_read,
    self_builtin_mark_as_read,
    self_mark_as_read_done
  },
  {
    "mark-as-spam",
    "spam",
    /* translators: header capitalization */
    N_("Mark as Spam"),
    N_("Unable to mark message as spam"),
    self_builtin_can_mark_as_spam,
    self_builtin_mark_as_spam,
    self_mark_as_spam_done
  },
  {
    "delete",
    "delete",
    /* translators: header capitalization */
    N_("Delete"),
    N_("Unable to mark message as spam"),
    self_builtin_can_delete,
    self_builtin_delete,
    self_delete_done
  }
};
%}

class MN:Message from G:Object
{
  /*
   * In order to not create reference cycles, we do not hold a
   * reference to the mailbox. The code is arranged so that a message
   * cannot survive its containing mailbox (whenever the mailbox is
   * removed, subsystems handle the messages-changed signal and
   * dereference the mailbox messages).
   */
  public MNMailbox *mailbox;
  property POINTER mailbox (flags = CONSTRUCT_ONLY, link, type = MNMailbox *);

  /* sent time, may be 0 */
  public time_t sent_time;
  property ULONG sent_time (link, flags = CONSTRUCT_ONLY | MN_MESSAGE_PARAM_EXPORT, link, type = time_t);

  /*
   * The application-wise message identifier. It is used by various
   * subsystems to test the equality of two messages (the MNMessage
   * instance pointers cannot be compared since they can change across
   * checks).
   *
   * Uniqueness is highly desired but not required. Nothing
   * catastrophical will happen if an ID clash occurs.
   *
   * This field is never NULL.
   */
  public char *id destroywith g_free;
  property STRING id (link, flags = CONSTRUCT_ONLY | MN_MESSAGE_PARAM_EXPORT);

  /*
   * The mailbox-wise message identifier. It is used by MNMailbox to
   * cache the message. It is not cached using the application-wise ID
   * because for most backends, retrieving that ID requires to read
   * the message, which obviously defeats the purpose of caching.
   *
   * If set, it should be unique across all the messages of the
   * containing mailbox, but nothing catastrophical will happen if an
   * ID clash occurs.
   *
   * If NULL, the message will not be cached.
   */
  public char *mid destroywith g_free;
  property STRING mid (link, flags = CONSTRUCT_ONLY);

  /* always set */
  public char *from destroywith g_free;
  property STRING from (link, flags = CONSTRUCT_ONLY | MN_MESSAGE_PARAM_EXPORT);

  /* always set */
  public char *subject destroywith g_free;
  property STRING subject (link, flags = CONSTRUCT_ONLY | MN_MESSAGE_PARAM_EXPORT);

  /* may be NULL */
  public char *uri destroywith g_free;
  property STRING uri (link, flags = CONSTRUCT_ONLY | MN_MESSAGE_PARAM_EXPORT);

  /* may be NULL */
  property STRING filename (flags = MN_MESSAGE_PARAM_EXPORT)
    get {
      g_value_take_string(VAL, self->uri ? gnome_vfs_get_local_path_from_uri(self->uri) : NULL);
    };

  public MNMessageFlags flags;
  property UINT flags (link, flags = CONSTRUCT_ONLY);

  public MNMessageAction *
    get_action (const char *name (check null))
  {
    static GHashTable *actions = NULL;

    if (! actions)
      {
	int i;

	actions = g_hash_table_new(g_str_hash, g_str_equal);

	for (i = 0; i < G_N_ELEMENTS(message_actions); i++)
	  {
	    const MNMessageAction *action = &message_actions[i];

	    g_hash_table_insert(actions, (gpointer) action->name, (gpointer) action);
	  }
      }

    return g_hash_table_lookup(actions, name);
  }

  constructor (self)
  {
    g_assert(MN_IS_MAILBOX(self->mailbox));

    if (! self->id)
      {
	GString *id;

	/* no ID was provided, try to generate a persistent one */

	id = g_string_new(NULL);

	if (self->sent_time > 0)
	  g_string_append_printf(id, ":sent-time:%i:", (int) self->sent_time);
	if (self->from)
	  g_string_append_printf(id, ":from:%s:", self->from);
	if (self->subject)
	  g_string_append_printf(id, ":subject:%s:", self->subject);

	if (! *id->str)
	  {
	    static int unique = 0;

	    /*
	     * We could not generate a persistent ID. Fallback to a
	     * non-persistent one.
	     */

	    g_string_append_printf(id, "%i", g_atomic_int_exchange_and_add(&unique, 1));
	  }

	self->id = g_string_free(id, FALSE);
      }

    /* these fields must only be filled after we have generated an ID */

    if (! self->from)
      self->from = g_strdup("");
    if (! self->subject)
      self->subject = g_strdup("");
  }

  private gboolean
    subst_command_cb (const char *name, char **value, gpointer data)
  {
    Self *self = data;
    GParamSpec **properties;
    unsigned int n_properties;
    gboolean status = FALSE;
    int i;

    properties = g_object_class_list_properties(G_OBJECT_GET_CLASS(self), &n_properties);
    for (i = 0; i < n_properties; i++)
      if ((properties[i]->flags & MN_MESSAGE_PARAM_EXPORT) != 0
	  && ! strcmp(g_param_spec_get_name(properties[i]), name))
	{
	  GValue gvalue = { 0, };

	  g_value_init(&gvalue, G_PARAM_SPEC_VALUE_TYPE(properties[i]));
	  g_object_get_property(G_OBJECT(self), name, &gvalue);

	  *value = mn_g_value_to_string(&gvalue);
	  g_value_unset(&gvalue);

	  status = TRUE;
	  break;
	}
    g_free(properties);

    return status;
  }

  private gboolean
    execute_command_real (self,
			  const char *command (check null),
			  GError **err)
  {
    char *subst;
    int status;

    subst = mn_subst_command(command, self_subst_command_cb, self, err);
    if (! subst)
      return FALSE;

    status = gnome_execute_shell(NULL, subst);
    g_free(subst);

    if (status < 0)
      {
	g_set_error(err, 0, 0, "%s", g_strerror(errno));
	return FALSE;
      }

    return TRUE;
  }

  /*
   * Returns TRUE if a custom action was found. Sets @err is the
   * execution of the custom action failed.
   */
  private gboolean
    execute_command (self, const char *id (check null), GError **err)
  {
    char *command;
    GError *tmp_err = NULL;

    command = mn_mailbox_get_command(self->mailbox, id);
    if (! command)
      return FALSE;

    if (! self_execute_command_real(self, command, &tmp_err))
      {
	g_set_error(err, 0, 0, _("Unable to execute \"%s\": %s."), command, tmp_err->message);
	g_error_free(tmp_err);
      }

    g_free(command);
    return TRUE;
  }

  public gboolean
    can_perform_action (self, MNMessageAction *action (check null))
  {
    return mn_mailbox_has_command(self->mailbox, action->name)
      || action->can_perform(self);
  }

  public void
    perform_action (self,
		    MNMessageAction *action (check null),
		    MNMessageActionResultCallback callback (check null),
		    gpointer data)
  {
    GError *err = NULL;

    if (self_execute_command(self, action->name, &err))
      self_action_done_real(self, action, err, callback, data);
    else
      {
	MNMessageActionRequest *request;

	request = g_new0(MNMessageActionRequest, 1);
	request->message = g_object_ref(self);
	request->action = action;
	request->callback = callback;
	request->data = data;

	action->perform(self, request);
      }
  }

  protected void
    perform_action_in_thread (MNMessageActionRequest *request (check null),
			      MNMessageActionPerformCallback callback (check null),
			      gpointer user_data)
  {
    PerformInfo *info;

    info = g_new0(PerformInfo, 1);
    info->request = request;
    info->callback = callback;
    info->user_data = user_data;

    g_object_ref(request->message);
    g_object_ref(request->message->mailbox);

    mn_thread_create((GThreadFunc) self_perform_action_in_thread_cb, info);
  }

  private void
    perform_action_in_thread_cb (PerformInfo *info)
  {
    GError *err;

    err = info->callback(info->request->message, info->user_data);

    GDK_THREADS_ENTER();

    self_action_done(info->request, err);

    g_object_unref(info->request->message->mailbox);
    g_object_unref(info->request->message);

    gdk_flush();
    GDK_THREADS_LEAVE();
  }

  private void
    action_done_real (self,
		      MNMessageAction *action (check null),
		      GError *err,
		      MNMessageActionResultCallback callback,
		      gpointer data)
  {
    action->done(self, err);
    callback(action, err, data);
  }

  protected void
    action_done (MNMessageActionRequest *request (check null), GError *err)
  {
    Self *self = request->message;

    self_action_done_real(self, request->action, err, request->callback, request->data);

    g_object_unref(request->message);
    g_free(request);
  }

  public GQuark
    action_error_quark (void)
  {
    return g_quark_from_static_string("mn-message-action-error");
  }

  virtual private gboolean
    builtin_can_open (self)
  {
    return self->uri != NULL;
  }

  virtual private void
    builtin_open (self, MNMessageActionRequest *request)
  {
    GError *err = NULL;

    gnome_url_show(self->uri, &err);

    self_action_done(request, err);
  }

  private void
    open_done (self, GError *err)
  {
    if (! err)
      self_consider_as_read(self); /* [1] */
  }

  virtual private gboolean
    builtin_can_mark_as_read (self)
  {
    return SELF_GET_CLASS(self)->builtin_mark_as_read != NULL;
  }

  virtual private void
    builtin_mark_as_read (self, MNMessageActionRequest *request);

  private void
    mark_as_read_done (self, GError *err)
  {
    if (! err)
      self_consider_as_read(self); /* [1] */
  }

  virtual private gboolean
    builtin_can_mark_as_spam (self)
  {
    return SELF_GET_CLASS(self)->builtin_mark_as_spam != NULL;
  }

  virtual private void
    builtin_mark_as_spam (self, MNMessageActionRequest *request);

  private void
    mark_as_spam_done (self, GError *err)
  {
    if (! err)
      self_consider_as_read(self); /* [1] */
  }

  virtual private gboolean
    builtin_can_delete (self)
  {
    return SELF_GET_CLASS(self)->builtin_delete != NULL;
  }

  virtual private void
    builtin_delete (self, MNMessageActionRequest *request);

  private void
    delete_done (self, GError *err)
  {
    if (! err)
      self_consider_as_read(self); /* [1] */
  }

  public void
    consider_as_read (self)
  {
    GSList *list;
    GSList *l;
    gboolean exists = FALSE;

    list = mn_conf_get_string_list(MN_CONF_MESSAGES_CONSIDERED_AS_READ);

    MN_LIST_FOREACH(l, list)
      {
	const char *id = l->data;

	if (! strcmp(id, self->id))
	  {
	    exists = TRUE;
	    break;
	  }
      }

    if (! exists)
      {
	list = g_slist_prepend(list, g_strdup(self->id));

	mn_conf_set_string_list(MN_CONF_MESSAGES_CONSIDERED_AS_READ, list);
      }

    mn_g_slist_free_deep(list);
  }

  /*
   * Atomically considers a list of messages as read, setting the
   * GConf list only once rather than for each message.
   */
  public void
    consider_as_read_list (GList *messages)
  {
    GHashTable *set;
    unsigned int old_size;
    GList *l;

    set = mn_conf_get_string_hash_set(MN_CONF_MESSAGES_CONSIDERED_AS_READ);

    old_size = g_hash_table_size(set);

    MN_LIST_FOREACH(l, messages)
      {
	MNMessage *message = l->data;

	g_hash_table_replace(set, g_strdup(message->id), GINT_TO_POINTER(TRUE));
      }

    if (g_hash_table_size(set) != old_size)
      mn_conf_set_string_hash_set(MN_CONF_MESSAGES_CONSIDERED_AS_READ, set);

    g_hash_table_destroy(set);
  }

  public MNMessage *
    new (MN:Mailbox *mailbox (check null type),
	 time_t sent_time,
	 const char *id,
	 const char *mid,
	 const char *from,
	 const char *subject,
	 const char *uri,
	 MNMessageFlags flags)
  {
    return GET_NEW_VARG(MN_MESSAGE_PROP_MAILBOX(mailbox),
			MN_MESSAGE_PROP_SENT_TIME(sent_time),
			MN_MESSAGE_PROP_ID((char *) id),
			MN_MESSAGE_PROP_MID((char *) mid),
			MN_MESSAGE_PROP_FROM((char *) from),
			MN_MESSAGE_PROP_SUBJECT((char *) subject),
			MN_MESSAGE_PROP_URI((char *) uri),
			MN_MESSAGE_PROP_FLAGS(flags),
			NULL);
  }

  public xmlNode *
    xml_node_new (self)
  {
    xmlNode *node;

    node = xmlNewNode(NULL, "message");

    xmlSetProp(node, "mailbox", self->mailbox->runtime_name);

    if ((self->flags & MN_MESSAGE_NEW) != 0)
      xmlSetProp(node, "new", "true");

    mn_xml_export_properties(G_OBJECT(self), node);

    return node;
  }
}

/*
 * [1]: there can be a slight (or large if polling is in effect) delay
 * between executing an action which should cause a message to
 * disappear from MN (open it, mark it as read, etc) and having the
 * next mail check catch the change. By adding the message to the
 * considered-as-read GConf list, this delay is concealed from the
 * user.
 */