File: main.c

package info (click to toggle)
balsa 0.6.0-1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,044 kB
  • ctags: 3,648
  • sloc: ansic: 36,750; sh: 7,458; makefile: 518; sed: 93
file content (385 lines) | stat: -rw-r--r-- 9,355 bytes parent folder | download
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
/* Balsa E-Mail Client
 * Copyright (C) 1997-1999 Jay Painter and Stuart Parmenter
 *
 * 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 2, 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., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */

#include "config.h"

#include <gnome.h>
#include <libgnorba/gnorba.h>
#include <orb/orbit.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef BALSA_USE_THREADS
#include <pthread.h>
#endif

#include "balsa-app.h"
#include "balsa-icons.h"
#include "main-window.h"
#include "libbalsa.h"
#include "misc.h"
#include "save-restore.h"
#include "main.h"
#include "balsa-impl.c"

#include "libinit_balsa/init_balsa.h"

#ifdef BALSA_USE_THREADS
#include "threads.h"


/* Globals for Thread creation, messaging, pipe I/O */
pthread_t			get_mail_thread;
pthread_t                       send_mail;
pthread_mutex_t			mailbox_lock;
pthread_mutex_t                 send_messages_lock;
int				checking_mail;
int                             sending_mail;
int				mail_thread_pipes[2];
int                             send_thread_pipes[2];
GIOChannel 		*mail_thread_msg_send;
GIOChannel 		*mail_thread_msg_receive;
GIOChannel              *send_thread_msg_send;
GIOChannel              *send_thread_msg_receive;


static void threads_init( gboolean init );
#endif /* BALSA_USE_THREADS */

static void balsa_init (int argc, char **argv);
static void config_init (void);
static void mailboxes_init (void);

void Exception (CORBA_Environment *);

void
Exception (CORBA_Environment * ev)
{
  switch (ev->_major)
    {
    case CORBA_SYSTEM_EXCEPTION:
      g_log ("BALSA Server", G_LOG_LEVEL_DEBUG, "CORBA system exception %s.\n",
	     CORBA_exception_id (ev));
      exit (1);
    case CORBA_USER_EXCEPTION:
      g_log ("BALSA Server", G_LOG_LEVEL_DEBUG, "CORBA user exception: %s.\n",
	     CORBA_exception_id (ev));
      exit (1);
    default:
      break;
    }
}


static void
balsa_init (int argc, char **argv)
{
  CORBA_ORB orb;
  CORBA_Environment ev;
  balsa_mail_send balsa_servant;
  PortableServer_POA root_poa;
  PortableServer_POAManager pm;
  static struct poptOption options[] = {
         {"checkmail", 'c', POPT_ARG_NONE, &(balsa_app.check_mail_upon_startup), 0, N_("Get new mail on startup"), NULL},
         {NULL, '\0', 0, NULL, 0} /* end the list */
  };

  CORBA_exception_init (&ev);

  orb = gnome_CORBA_init_with_popt_table ("balsa", VERSION,
			  &argc, argv, options, 0, NULL,
			  GNORBA_INIT_SERVER_FUNC,
			  &ev);

  Exception (&ev);

  root_poa = (PortableServer_POA) CORBA_ORB_resolve_initial_references (orb,
							    "RootPOA", &ev);
  Exception (&ev);

  balsa_servant = impl_balsa_mail_send__create (root_poa, &ev);
  Exception (&ev);

  pm = PortableServer_POA__get_the_POAManager (root_poa, &ev);
  Exception (&ev);

  PortableServer_POAManager_activate (pm, &ev);
  Exception (&ev);

  goad_server_register (CORBA_OBJECT_NIL,
			balsa_servant, "balsa_mail_send", "server", &ev);
}

static void
config_init (void)
{
  if (config_load (BALSA_CONFIG_FILE) == FALSE)
    {
      balsa_init_begin ();
      //return;
    }

  /* Load all the global settings.  If there's an error, then some crucial
     piece of the global settings was not available, and we need to run
     balsa-init. */
  if (config_global_load () == FALSE)
    {
      fprintf (stderr, "*** config_global_load failed\n");
      balsa_init_begin ();
      return;
    }
}

static void
mailboxes_init (void)
{
  /* initalize our mailbox access crap */
  if (do_load_mailboxes () == FALSE)
    {
      fprintf (stderr, "*** error loading mailboxes\n");
      balsa_init_begin ();
      return;
    }

  /* At this point, if inbox/outbox/trash are still null, then we
     were not able to locate the settings for them anywhere in our
     configuartion and should run balsa-init. */
  if (balsa_app.inbox == NULL || balsa_app.outbox == NULL ||
      balsa_app.sentbox == NULL || balsa_app.trash == NULL ||
      balsa_app.draftbox == NULL)
    {
      fprintf (stderr, 
               "*** One of inbox/outbox/sentbox/draftbox/trash is NULL\n");
      balsa_init_begin ();
      return;
    }
}


#ifdef BALSA_USE_THREADS
void
threads_init( gboolean init )
{
  if( init )
  {
    g_thread_init( NULL );
    pthread_mutex_init( &mailbox_lock, NULL );
    pthread_mutex_init( &send_messages_lock, NULL );
    checking_mail = 0;
    sending_mail = 0;
	if( pipe( mail_thread_pipes) < 0 )
	{
	   g_log ("BALSA Init", G_LOG_LEVEL_DEBUG, "Error opening pipes.\n" );
	}
	mail_thread_msg_send = g_io_channel_unix_new ( mail_thread_pipes[1] );
	mail_thread_msg_receive = g_io_channel_unix_new ( mail_thread_pipes[0] );
	g_io_add_watch ( mail_thread_msg_receive, G_IO_IN,
					(GIOFunc) mail_progress_notify_cb,
					NULL );

	if( pipe( send_thread_pipes) < 0 )
	{
	   g_log ("BALSA Init", G_LOG_LEVEL_DEBUG, "Error opening pipes.\n" );
	}
	send_thread_msg_send = g_io_channel_unix_new ( send_thread_pipes[1] );
	send_thread_msg_receive = g_io_channel_unix_new ( send_thread_pipes[0] );
	g_io_add_watch ( send_thread_msg_receive, G_IO_IN,
					(GIOFunc) send_progress_notify_cb,
					NULL );
					
  }
  else
  {
    pthread_mutex_destroy( &mailbox_lock );
    pthread_mutex_destroy( &send_messages_lock );
  }
}
#endif /* BALSA_USE_THREADS */



int
main (int argc, char *argv[])
{
  GtkWidget *window;

  /* Initialize the i18n stuff */
  bindtextdomain (PACKAGE, GNOMELOCALEDIR);
  textdomain (PACKAGE);
 
  balsa_init (argc, argv);

  balsa_app_init ();

  /* checking for valid config files */
  config_init ();

  /* load mailboxes */
  config_mailboxes_init ();
  mailboxes_init ();

#ifdef BALSA_USE_THREADS
  /* initiate thread mutexs, variables */
  threads_init( TRUE );
#endif  

  /* create all the pretty icons that balsa uses that
   * arn't part of gnome-libs */
  balsa_icons_init ();

  gnome_triggers_do ("", "program", "balsa", "startup", NULL);

  window = balsa_window_new();
  gtk_widget_show(window);

  gtk_main ();

#ifdef BALSA_USE_THREADS
  threads_init( FALSE );
#endif

  return 0;
}

static gboolean
close_all_mailboxes (GNode * node, gpointer data)
{
  Mailbox *mailbox;
  MailboxNode *mbnode;

  if (node->data)
    {
      mbnode = (MailboxNode *) node->data;

      if (mbnode)
	{
	  gchar *tmpfile;

	  if (mbnode->IsDir)
	    {
	      if (mbnode->expanded)
		{
		  tmpfile = g_strdup_printf ("%s/.expanded", mbnode->name);
		  if (access (tmpfile, F_OK) == -1)
		    creat (tmpfile, S_IRUSR | S_IWUSR);
		  g_free (tmpfile);
		}
	      else
		{
		  tmpfile = g_strdup_printf ("%s/.expanded", mbnode->name);
		  if (access (tmpfile, F_OK) != -1)
		    unlink (tmpfile);
		  g_free (tmpfile);
		}
	    }

	  mailbox = mbnode->mailbox;

	  if (!mailbox)
	    return FALSE;

	  if (balsa_app.debug)
	    g_print ("Mailbox: %s Ref: %d\n", mailbox->name, mailbox->open_ref);

	  while (mailbox->open_ref > 0)
	    mailbox_open_unref (mailbox);
	}
    }
  return FALSE;
}

void
balsa_exit (void)
{
  Mailbox *mailbox;

  g_node_traverse (balsa_app.mailbox_nodes,
		   G_LEVEL_ORDER,
		   G_TRAVERSE_ALL,
		   10,
		   close_all_mailboxes,
		   NULL);

  mailbox = balsa_app.inbox;
  if (mailbox)
    {
      if (balsa_app.debug)
	g_print ("Mailbox: %s Ref: %d\n", mailbox->name, mailbox->open_ref);
      while (mailbox->open_ref > 0)
	mailbox_open_unref (mailbox);
    }

  mailbox = balsa_app.outbox;
  if (mailbox)
    {
      if (balsa_app.debug)
	g_print ("Mailbox: %s Ref: %d\n", mailbox->name, mailbox->open_ref);
      while (mailbox->open_ref > 0)
	mailbox_open_unref (mailbox);
    }

  mailbox = balsa_app.sentbox;
  if (mailbox)
    {
      if (balsa_app.debug)
	g_print ("Mailbox: %s Ref: %d\n", mailbox->name, mailbox->open_ref);
      while (mailbox->open_ref > 0)
	mailbox_open_unref (mailbox);
    }

  mailbox = balsa_app.draftbox;
  if (mailbox)
    {
      if (balsa_app.debug)
	g_print ("Mailbox: %s Ref: %d\n", mailbox->name, mailbox->open_ref);
      while (mailbox->open_ref > 0)
	mailbox_open_unref (mailbox);
    }

  mailbox = balsa_app.trash;
  if (mailbox)
    {
      if (balsa_app.debug)
	g_print ("Mailbox: %s Ref: %d\n", mailbox->name, mailbox->open_ref);
      while (mailbox->open_ref > 0)
	mailbox_open_unref (mailbox);
    }

  if (balsa_app.proplist)
    config_global_save ();

  gnome_sound_shutdown ();

  gtk_main_quit();
}

/* Eeew. But I'm tired of ifdefs. -- PGKW */
/* Don't EVER EVER EVER call this even for a joke -- it recurses. */
static void __lame_hack_to_avoid_unused_warnings( void );
typedef void (*__lame_funcptr)( void );
static void __lame_hack_to_avoid_unused_warnings( void ) 
{
	__lame_funcptr i_b_m_i__c = (__lame_funcptr) impl_balsa_mailbox_info__create;
	__lame_funcptr self = (__lame_funcptr) __lame_hack_to_avoid_unused_warnings;

	i_b_m_i__c();
	self();
}