aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/calendar-conduit-control-applet.c
blob: 3c5b2d1b9899a0c64fa05bd7fcfaf61f7b28a25d (plain) (blame)
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
/* Control applet ("capplet") for the gnome-pilot calendar conduit,         */
/* based on                                                                 */
/* gpilotd control applet ('capplet') for use with the GNOME control center */
/* $Id$ */

#include <pwd.h>
#include <sys/types.h>
#include <signal.h>
#include <gnome.h>
#include <ctype.h>

#include <config.h>
#include <capplet-widget.h>

#include <gpilotd/gpilotd-conduit-mgmt.h>
#include <gpilotd/gpilotd-app.h>
#include <gpilotd/gpilotd-app-dummy-callbacks.h>

#include "calendar-conduit.h"

/* tell changes callbacks to ignore changes or not */
static gboolean ignore_changes=FALSE;

/* capplet widget */
static GtkWidget *capplet=NULL;

/* host/device/pilot configuration windows */
GtkWidget *cfgOptionsWindow=NULL;
GtkWidget *cfgStateWindow=NULL;
GtkWidget *dialogWindow=NULL;

GnomePilotConduitMgmt *conduit;

static void doTrySettings(GtkWidget *widget, gpointer);
static void doRevertSettings(GtkWidget *widget, gpointer);
static void doSaveSettings(GtkWidget *widget, gpointer);

static void readStateCfg(GtkWidget *w,GCalConduitCfg *cfg);
static void setStateCfg(GtkWidget *w,GCalConduitCfg *cfg);
void about_cb (GtkWidget *, gpointer);
GCalConduitCfg *origState = NULL;
GCalConduitCfg *curState = NULL;

gint pilotId;
CORBA_Environment ev;

/* This array must be in the same order as enumerations
   in GnomePilotConduitSyncType as they are used as index.
   Custom type implies Disabled state.
*/
static gchar* sync_options[] ={ N_("Disabled"),
                N_("Synchronize"),
                N_("Copy From Pilot"),
                N_("Copy To Pilot"),
                N_("Merge From Pilot"),
                N_("Merge To Pilot")};
#define SYNC_OPTIONS_COUNT 6

static void 
setSettings(GCalConduitCfg* conduitCfg)
{
    if(conduitCfg->sync_type!=GnomePilotConduitSyncTypeCustom)
        gpilotd_conduit_mgmt_enable(conduit,pilotId,conduitCfg->sync_type);
    else
        gpilotd_conduit_mgmt_disable(conduit,pilotId);

    gcalconduit_save_configuration(conduitCfg);
}

static void
doTrySettings(GtkWidget *widget, gpointer whatever)
{
    readStateCfg(cfgStateWindow,curState);
    setSettings(curState);
}

static void
doSaveSettings(GtkWidget *widget, gpointer whatever)
{
    doTrySettings(widget,whatever);
}

static void
doCancelSettings(GtkWidget *widget, gpointer whatever)
{
    setSettings(origState);
}

static void
doRevertSettings(GtkWidget *widget, gpointer whatever)
{
    gcalconduit_destroy_configuration(&curState);
    curState = gcalconduit_dupe_configuration(origState);
    setStateCfg(cfgStateWindow,curState);
    setSettings(curState);
}

static void
insert_dir_callback (GtkEditable    *editable, const gchar    *text,
             gint len, gint *position, void *data)
{
    gint i;
    gchar *curname;

    curname = gtk_entry_get_text(GTK_ENTRY(editable));
    if (*curname == '\0' && len > 0) {
    if (isspace(text[0])) {
        gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
        return;
    }
    } else {
    for (i=0; i<len; i++) {
        if (isspace(text[i])) {
        gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), 
                         "insert_text");
        return;
        }
    }
    }
}
static void
insert_dir_callback2(GtkEditable    *editable, const gchar    *text,
              gint            length, gint           *position,
              void *data)
{
    if (!ignore_changes)
        capplet_widget_state_changed(CAPPLET_WIDGET(capplet), TRUE);
}

static void
clist_changed(GtkWidget *widget, gpointer data)
{
    if (!ignore_changes)
    capplet_widget_state_changed(CAPPLET_WIDGET(capplet), TRUE);
}
    
void about_cb (GtkWidget *widget, gpointer data) {
  GtkWidget *about;
  const gchar *authors[] = {_("Eskil Heyn Olsen <deity@eskil.dk>"),NULL};
  
  about = gnome_about_new(_("GnomeCalendar Conduit"), VERSION,
              _("(C) 1998"),
              authors,
              _("Configuration utility for the calendar conduit.\n"),
              _("gnome-calendar-conduit.png"));
  gtk_widget_show (about);
  
  return;
}

static void toggled_cb(GtkWidget *widget, gpointer data) {
  capplet_widget_state_changed(CAPPLET_WIDGET(capplet), TRUE);
}

/* called by the sync_type GtkOptionMenu */
static void
sync_action_selection(GtkMenuShell *widget, gpointer unused) 
{
    if (!ignore_changes) {
        capplet_widget_state_changed(CAPPLET_WIDGET (capplet), TRUE);
    }
}

/* called by the sync_type GtkOptionMenu */
static void
activate_sync_type(GtkMenuItem *widget, gpointer data)
{
    curState->sync_type = GPOINTER_TO_INT(data);
    if(!ignore_changes)
        capplet_widget_state_changed(CAPPLET_WIDGET(capplet), TRUE);
}

static GtkWidget
*createStateCfgWindow(void)
{
    GtkWidget *vbox, *table;
    GtkWidget *label;
    GtkWidget *optionMenu,*menuItem;
    GtkMenu   *menu;
    gint i;
    
    vbox = gtk_vbox_new(FALSE, GNOME_PAD);

    table =  gtk_hbox_new(FALSE, 0); 
    gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, GNOME_PAD);

    label = gtk_label_new(_("Synchronize Action"));
    gtk_box_pack_start(GTK_BOX(table), label, FALSE, FALSE, GNOME_PAD);    

    optionMenu=gtk_option_menu_new();
    gtk_object_set_data(GTK_OBJECT(vbox), "conduit_state", optionMenu);
    menu = GTK_MENU(gtk_menu_new());

    for (i=0; i<SYNC_OPTIONS_COUNT;i++) {
        sync_options[i]=_(sync_options[i]);
        menuItem = gtk_menu_item_new_with_label(sync_options[i]);
        gtk_widget_show(menuItem);
        gtk_signal_connect(GTK_OBJECT(menuItem),"activate",
                   GTK_SIGNAL_FUNC(activate_sync_type),
                   GINT_TO_POINTER(i));
        gtk_menu_append(menu,menuItem);
    }

    gtk_option_menu_set_menu(GTK_OPTION_MENU(optionMenu),GTK_WIDGET(menu));
    gtk_signal_connect(GTK_OBJECT(menu), "selection-done",
               GTK_SIGNAL_FUNC(sync_action_selection),
               NULL);
  
    gtk_box_pack_start(GTK_BOX(table), optionMenu, FALSE, FALSE, 0);    
    
    return vbox;
}

static void
setStateCfg(GtkWidget *widget,GCalConduitCfg *cfg)
{
    GtkOptionMenu *optionMenu;
    GtkMenu *menu;
    
    optionMenu = gtk_object_get_data(GTK_OBJECT(widget), "conduit_state");
    g_assert(optionMenu!=NULL);
    menu = GTK_MENU(gtk_option_menu_get_menu(optionMenu));

  
    ignore_changes = TRUE;
    /* Here were are relying on the items in menu being the same 
       order as in GnomePilotConduitSyncType. */
    gtk_option_menu_set_history(optionMenu,(int)cfg->sync_type);
    ignore_changes = FALSE;
}


static void
readStateCfg(GtkWidget *w,GCalConduitCfg *cfg)
{
/*
  GtkWidget *button;

  button  = gtk_object_get_data(GTK_OBJECT(w), "conduit_on_off");
  
  g_assert(button!=NULL);

  activated = GTK_TOGGLE_BUTTON(button)->active;
*/
}

static void
pilot_capplet_setup(void)
{
    GtkWidget *frame, *table;

    capplet = capplet_widget_new();

    table = gtk_table_new(1, 2, FALSE);
    gtk_container_border_width(GTK_CONTAINER(table), GNOME_PAD);
    gtk_container_add(GTK_CONTAINER(capplet), table); 

    frame = gtk_frame_new(_("Conduit state"));
    gtk_container_border_width(GTK_CONTAINER(frame), GNOME_PAD_SMALL);
    gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 1, 0, 1);
    cfgStateWindow = createStateCfgWindow();
    gtk_container_add(GTK_CONTAINER(frame), cfgStateWindow);

    gtk_signal_connect(GTK_OBJECT(capplet), "try",
               GTK_SIGNAL_FUNC(doTrySettings), NULL);
    gtk_signal_connect(GTK_OBJECT(capplet), "revert",
               GTK_SIGNAL_FUNC(doRevertSettings), NULL);
    gtk_signal_connect(GTK_OBJECT(capplet), "ok",
               GTK_SIGNAL_FUNC(doSaveSettings), NULL);
    gtk_signal_connect(GTK_OBJECT(capplet), "cancel",
               GTK_SIGNAL_FUNC(doCancelSettings), NULL);
    gtk_signal_connect(GTK_OBJECT(capplet), "help",
               GTK_SIGNAL_FUNC(about_cb), NULL);


    setStateCfg(cfgStateWindow,curState);

    gtk_widget_show_all(capplet);
}

static void 
run_error_dialog(gchar *mesg,...) {
    char tmp[80];
    va_list ap;
    
    va_start(ap,mesg);
    vsnprintf(tmp,79,mesg,ap);
    dialogWindow = gnome_message_box_new(mesg,GNOME_MESSAGE_BOX_ERROR,GNOME_STOCK_BUTTON_OK,NULL);
    gnome_dialog_run_and_close(GNOME_DIALOG(dialogWindow));
    va_end(ap);
}

gint get_pilot_id_from_gpilotd() {
    gint *pilots;
    int i;
  
    i=0;
    gpilotd_get_pilot_ids(&pilots);
    if(pilots) {
        while(pilots[i]!=-1) { g_message("pilot %d = \"%d\"",i,pilots[i]); i++; }
        if(i==0) {
            run_error_dialog(_("No pilot configured, please choose the\n'Pilot Link Properties' capplet first."));
            return -1;
        } else {
            if(i==1) 
                return pilots[0];
            else {
                g_message("too many pilots...");
                return pilots[0];
            }
        }
    } else {
        run_error_dialog(_("No pilot configured, please choose the\n'Pilot Link Properties' capplet first."));
        return -1;
    }    
}

int
main( int argc, char *argv[] )
{
    /* we're a capplet */
    gnome_capplet_init ("calendar conduit control applet", NULL, argc, argv, 
                NULL,
                0, NULL);

    /* put all code to set things up in here */
    conduit = gpilotd_conduit_mgmt_new("calendar_conduit");

    /* get pilot name from gpilotd */
    /* 1. initialize the gpilotd connection */
    if (gpilotd_init(&argc,argv)!=0) {
        run_error_dialog(_("Cannot initialze the GnomePilot Daemon"));
        g_error(_("Cannot initialze the GnomePilot Daemon"));
        return -1;
    }
    
    /* 2 connect to gpilotd */
    if (gpilotd_connect()!=0) {
        run_error_dialog(_("Cannot connect to the GnomePilot Daemon"));
        g_error(_("Cannot connect to the GnomePilot Daemon"));
        return -1;
    }
    
    pilotId = get_pilot_id_from_gpilotd();
    if(!pilotId) return -1;

    /* put all code to set things up in here */
    gcalconduit_load_configuration(&origState,pilotId);
    gpilotd_conduit_mgmt_get_sync_type(conduit,pilotId,&origState->sync_type);

    curState = gcalconduit_dupe_configuration(origState);
    
    pilot_capplet_setup();


    /* done setting up, now run main loop */
    capplet_gtk_main();
    return 0;
}