Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/gsm-client-xsmp.c
blob: 04c7de47e70cc1c1cc8a03c67fef597ad8529499 (plain)
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* client-xsmp.c
 * Copyright (C) 2007 Novell, Inc.
 *
 * 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 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
 * Lesser 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "gsm-client-xsmp.h"
#include "gsm-session.h"

/* FIXME */
#define GsmDesktopFile "_Gsm_DesktopFile"

static gboolean client_iochannel_watch  (GIOChannel   *channel,
					 GIOCondition  condition,
					 gpointer      data);
static gboolean client_protocol_timeout (gpointer      data);

static void set_description (GsmClientXSMP *xsmp);

static const char *xsmp_get_client_id       (GsmClient *client);
static pid_t       xsmp_get_pid             (GsmClient *client);
static char       *xsmp_get_desktop_file    (GsmClient *client);
static char       *xsmp_get_restart_command (GsmClient *client);
static char       *xsmp_get_discard_command (GsmClient *client);
static gboolean    xsmp_get_autorestart     (GsmClient *client);

static void xsmp_finalize             (GObject   *object);
static void xsmp_restart              (GsmClient *client,
                                       GError   **error);
static void xsmp_save_yourself        (GsmClient *client,
				       gboolean   save_state);
static void xsmp_save_yourself_phase2 (GsmClient *client);
static void xsmp_interact             (GsmClient *client);
static void xsmp_shutdown_cancelled   (GsmClient *client);
static void xsmp_die                  (GsmClient *client);

G_DEFINE_TYPE (GsmClientXSMP, gsm_client_xsmp, GSM_TYPE_CLIENT)

static void
gsm_client_xsmp_init (GsmClientXSMP *xsmp)
{
  ;
}

static void
gsm_client_xsmp_class_init (GsmClientXSMPClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GsmClientClass *client_class = GSM_CLIENT_CLASS (klass);

  object_class->finalize             = xsmp_finalize;

  client_class->get_client_id        = xsmp_get_client_id;
  client_class->get_pid              = xsmp_get_pid;
  client_class->get_desktop_file     = xsmp_get_desktop_file;
  client_class->get_restart_command  = xsmp_get_restart_command;
  client_class->get_discard_command  = xsmp_get_discard_command;
  client_class->get_autorestart      = xsmp_get_autorestart;

  client_class->restart              = xsmp_restart;
  client_class->save_yourself        = xsmp_save_yourself;
  client_class->save_yourself_phase2 = xsmp_save_yourself_phase2;
  client_class->interact             = xsmp_interact;
  client_class->shutdown_cancelled   = xsmp_shutdown_cancelled;
  client_class->die                  = xsmp_die;
}

GsmClientXSMP *
gsm_client_xsmp_new (IceConn ice_conn)
{
  GsmClientXSMP *xsmp;
  GIOChannel *channel;
  int fd;

  xsmp = g_object_new (GSM_TYPE_CLIENT_XSMP, NULL);
  xsmp->props = g_ptr_array_new ();

  xsmp->ice_conn = ice_conn;
  xsmp->current_save_yourself = -1;
  xsmp->next_save_yourself = -1;

  fd = IceConnectionNumber (ice_conn);
  fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC);

  channel = g_io_channel_unix_new (fd);
  xsmp->watch_id = g_io_add_watch (channel, G_IO_IN | G_IO_ERR,
				   client_iochannel_watch, xsmp);
  g_io_channel_unref (channel);

  xsmp->protocol_timeout = g_timeout_add_seconds (5, client_protocol_timeout, xsmp);

  set_description (xsmp);
  g_debug ("New client '%s'", xsmp->description);

  return xsmp;
}

static void
xsmp_finalize (GObject *object)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) object;

  g_debug ("xsmp_finalize (%s)", xsmp->description);

  if (xsmp->watch_id)
    g_source_remove (xsmp->watch_id);

  if (xsmp->conn)
    SmsCleanUp (xsmp->conn);
  else
    IceCloseConnection (xsmp->ice_conn);

  if (xsmp->protocol_timeout)
    g_source_remove (xsmp->protocol_timeout);

  G_OBJECT_CLASS (gsm_client_xsmp_parent_class)->finalize (object);
}

static gboolean
client_iochannel_watch (GIOChannel   *channel,
			GIOCondition  condition,
			gpointer      data)
{
  GsmClient *client = data;
  GsmClientXSMP *xsmp = data;

  switch (IceProcessMessages (xsmp->ice_conn, NULL, NULL))
    {
    case IceProcessMessagesSuccess:
      return TRUE;

    case IceProcessMessagesIOError:
      g_debug ("IceProcessMessagesIOError on '%s'", xsmp->description);
      gsm_client_disconnected (client);
      return FALSE;

    case IceProcessMessagesConnectionClosed:
      g_debug ("IceProcessMessagesConnectionClosed on '%s'",
	       xsmp->description);
      return FALSE;

    default:
      g_assert_not_reached ();
    }
}

/* Called if too much time passes between the initial connection and
 * the XSMP protocol setup.
 */
static gboolean
client_protocol_timeout (gpointer data)
{
  GsmClient *client = data;
  GsmClientXSMP *xsmp = data;

  g_debug ("client_protocol_timeout for client '%s' in ICE status %d",
	   xsmp->description, IceConnectionStatus (xsmp->ice_conn));
  gsm_client_disconnected (client);

  return FALSE;
}

static Status
register_client_callback (SmsConn    conn,
			  SmPointer  manager_data,
			  char      *previous_id)
{
  GsmClient *client = manager_data;
  GsmClientXSMP *xsmp = manager_data;
  char *id;

  g_debug ("Client '%s' received RegisterClient(%s)", 
           xsmp->description,
	   previous_id ? previous_id : "NULL");

  id = gsm_session_register_client (global_session, client, previous_id);

  if (id == NULL)
    {
      g_debug ("  rejected: invalid previous_id");
      free (previous_id);
      return FALSE;
    }

  xsmp->id = id;

  set_description (xsmp);

  g_debug ("Sending RegisterClientReply to '%s'", xsmp->description);

  SmsRegisterClientReply (conn, xsmp->id);

  if (!previous_id)
    {
      /* Send the initial SaveYourself. */
      g_debug ("Sending initial SaveYourself");
      SmsSaveYourself (conn, SmSaveLocal, False, SmInteractStyleNone, False);
      xsmp->current_save_yourself = SmSaveLocal;

      free (previous_id);
    }

  return TRUE;
}

static void
do_save_yourself (GsmClientXSMP *xsmp, int save_type)
{
  if (xsmp->next_save_yourself != -1)
    {
      /* Either we're currently doing a shutdown and there's a checkpoint
       * queued after it, or vice versa. Either way, the new SaveYourself
       * is redundant.
       */
      g_debug ("  skipping redundant SaveYourself for '%s'",
	       xsmp->description);
    }
  else if (xsmp->current_save_yourself != -1)
    {
      g_debug ("  queuing new SaveYourself for '%s'",
	       xsmp->description);
      xsmp->next_save_yourself = save_type;
    }
  else
    {
      xsmp->current_save_yourself = save_type;

      switch (save_type)
	{
	case SmSaveLocal:
	  /* Save state */
	  SmsSaveYourself (xsmp->conn, SmSaveLocal, FALSE,
			   SmInteractStyleNone, FALSE);
	  break;

	default:
	  /* Logout */
	  SmsSaveYourself (xsmp->conn, save_type, TRUE,
			   SmInteractStyleAny, FALSE);
	  break;
	}
    }
}

static void
save_yourself_request_callback (SmsConn   conn,
                                SmPointer manager_data,
                                int       save_type,
                                Bool      shutdown,
                                int       interact_style,
                                Bool      fast,
                                Bool      global)
{
  GsmClientXSMP *xsmp = manager_data;

  g_debug ("Client '%s' received SaveYourselfRequest(%s, %s, %s, %s, %s)",
	   xsmp->description,
	   save_type == SmSaveLocal ? "SmSaveLocal" :
	   save_type == SmSaveGlobal ? "SmSaveGlobal" : "SmSaveBoth",
	   shutdown ? "Shutdown" : "!Shutdown",
	   interact_style == SmInteractStyleAny ? "SmInteractStyleAny" :
	   interact_style == SmInteractStyleErrors ? "SmInteractStyleErrors" :
	   "SmInteractStyleNone", fast ? "Fast" : "!Fast",
	   global ? "Global" : "!Global");

  /* Examining the g_debug above, you can see that there are a total
   * of 72 different combinations of options that this could have been
   * called with. However, most of them are stupid.
   *
   * If @shutdown and @global are both TRUE, that means the caller is
   * requesting that a logout message be sent to all clients, so we do
   * that. We use @fast to decide whether or not to show a
   * confirmation dialog. (This isn't really what @fast is for, but
   * the old gnome-session and ksmserver both interpret it that way,
   * so we do too.) We ignore @save_type because we pick the correct
   * save_type ourselves later based on user prefs, dialog choices,
   * etc, and we ignore @interact_style, because clients have not used
   * it correctly consistently enough to make it worth honoring.
   *
   * If @shutdown is TRUE and @global is FALSE, the caller is
   * confused, so we ignore the request.
   *
   * If @shutdown is FALSE and @save_type is SmSaveGlobal or
   * SmSaveBoth, then the client wants us to ask some or all open
   * applications to save open files to disk, but NOT quit. This is
   * silly and so we ignore the request.
   *
   * If @shutdown is FALSE and @save_type is SmSaveLocal, then the
   * client wants us to ask some or all open applications to update
   * their current saved state, but not log out. At the moment, the
   * code only supports this for the !global case (ie, a client
   * requesting that it be allowed to update *its own* saved state,
   * but not having everyone else update their saved state).
   */

  if (shutdown && global)
    {
      g_debug ("  initiating shutdown");
/*      gsm_session_initiate_shutdown (global_session, 
                                     !fast, 
                                     GSM_SESSION_LOGOUT_TYPE_LOGOUT);
*/
    }
  else if (!shutdown && !global)
    {
      g_debug ("  initiating checkpoint");
      do_save_yourself (xsmp, SmSaveLocal);
    }
  else
    g_debug ("  ignoring");
}

static void 
xsmp_restart (GsmClient *client, GError **error)
{
  char *restart_cmd = gsm_client_get_restart_command (client);

  g_spawn_command_line_async (restart_cmd, error);

  g_free (restart_cmd);
}

static void
xsmp_save_yourself (GsmClient *client, gboolean save_state)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *)client;

  g_debug ("xsmp_save_yourself ('%s', %s)", xsmp->description,
	   save_state ? "True" : "False");

  do_save_yourself (xsmp, save_state ? SmSaveBoth : SmSaveGlobal);
}

static void
save_yourself_phase2_request_callback (SmsConn   conn,
                                       SmPointer manager_data)
{
  GsmClient *client = manager_data;
  GsmClientXSMP *xsmp = manager_data;

  g_debug ("Client '%s' received SaveYourselfPhase2Request",
	   xsmp->description);

  if (xsmp->current_save_yourself == SmSaveLocal)
    {
      /* WTF? Anyway, if it's checkpointing, it doesn't have to wait
       * for anyone else.
       */
      SmsSaveYourselfPhase2 (xsmp->conn);
    }
  else
    gsm_client_request_phase2 (client);
}

static void
xsmp_save_yourself_phase2 (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *)client;

  g_debug ("xsmp_save_yourself_phase2 ('%s')", xsmp->description);

  SmsSaveYourselfPhase2 (xsmp->conn);
}

static void
interact_request_callback (SmsConn   conn,
                           SmPointer manager_data,
                           int       dialog_type)
{
  GsmClient *client = manager_data;
  GsmClientXSMP *xsmp = manager_data;

  g_debug ("Client '%s' received InteractRequest(%s)", xsmp->description,
	   dialog_type == SmInteractStyleAny ? "Any" : "Errors");

  gsm_client_request_interaction (client);
}

static void
xsmp_interact (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;

  g_debug ("xsmp_interact ('%s')", xsmp->description);

  SmsInteract (xsmp->conn);
}

static void
interact_done_callback (SmsConn   conn,
                        SmPointer manager_data,
                        Bool      cancel_shutdown)
{
  GsmClient *client = manager_data;
  GsmClientXSMP *xsmp = manager_data;

  g_debug ("Client '%s' received InteractDone(cancel_shutdown = %s)",
	   xsmp->description, cancel_shutdown ? "True" : "False");

  gsm_client_interaction_done (client, cancel_shutdown);
}

static void
xsmp_shutdown_cancelled (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;

  g_debug ("xsmp_shutdown_cancelled ('%s')", xsmp->description);

  SmsShutdownCancelled (xsmp->conn);
}

static void
xsmp_die (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;

  g_debug ("xsmp_die ('%s')", xsmp->description);

  SmsDie (xsmp->conn);
}

static void
save_yourself_done_callback (SmsConn   conn,
			     SmPointer manager_data,
			     Bool      success)
{
  GsmClient *client = manager_data;
  GsmClientXSMP *xsmp = manager_data;

  g_debug ("Client '%s' received SaveYourselfDone(success = %s)",
	   xsmp->description, success ? "True" : "False");

  if (xsmp->current_save_yourself == SmSaveLocal)
    {
      xsmp->current_save_yourself = -1;
      SmsSaveComplete (xsmp->conn);
      gsm_client_saved_state (client);
    }
  else
    {
      xsmp->current_save_yourself = -1;
      gsm_client_save_yourself_done (client);
    }

  if (xsmp->next_save_yourself)
    {
      int save_type = xsmp->next_save_yourself;

      xsmp->next_save_yourself = -1;
      do_save_yourself (xsmp, save_type);
    }
}

static void
close_connection_callback (SmsConn     conn,
			   SmPointer   manager_data,
			   int         count,
			   char      **reason_msgs)
{
  GsmClient *client = manager_data;
  GsmClientXSMP *xsmp = manager_data;
  int i;

  g_debug ("Client '%s' received CloseConnection", xsmp->description);
  for (i = 0; i < count; i++)
    g_debug (" close reason: '%s'", reason_msgs[i]);
  SmFreeReasons (count, reason_msgs);

  gsm_client_disconnected (client);
}

static void
debug_print_property (SmProp *prop)
{
  GString *tmp;
  int i;

  switch (prop->type[0])
    {
    case 'C': /* CARD8 */
      g_debug ("  %s = %d", prop->name, *(unsigned char *)prop->vals[0].value);
      break;

    case 'A': /* ARRAY8 */
      g_debug ("  %s = '%s'", prop->name, (char *)prop->vals[0].value);
      break;

    case 'L': /* LISTofARRAY8 */
      tmp = g_string_new (NULL);
      for (i = 0; i < prop->num_vals; i++)
	{
	  g_string_append_printf (tmp, "'%.*s' ", prop->vals[i].length,
				  (char *)prop->vals[i].value);
	}
      g_debug ("  %s = %s", prop->name, tmp->str);
      g_string_free (tmp, TRUE);
      break;

    default:
      g_debug ("  %s = ??? (%s)", prop->name, prop->type);
      break;
    }
}

static SmProp *
find_property (GsmClientXSMP *client, const char *name, int *index)
{
  SmProp *prop;
  int i;

  for (i = 0; i < client->props->len; i++)
    {
      prop = client->props->pdata[i];

      if (!strcmp (prop->name, name))
	{
	  if (index)
	    *index = i;
	  return prop;
	}
    }

  return NULL;
}

static void
delete_property (GsmClientXSMP *client, const char *name)
{
  int index;
  SmProp *prop;

  prop = find_property (client, name, &index);
  if (!prop)
    return;	

#if 0
  /* This is wrong anyway; we can't unconditionally run the current
   * discard command; if this client corresponds to a GsmAppResumed,
   * and the current discard command is identical to the app's
   * discard_command, then we don't run the discard command now,
   * because that would delete a saved state we may want to resume
   * again later.
   */
  if (!strcmp (name, SmDiscardCommand))
    gsm_client_run_discard (client);
#endif

  g_ptr_array_remove_index_fast (client->props, index);
  SmFreeProperty (prop);
}

static void
set_properties_callback (SmsConn     conn,
                         SmPointer   manager_data,
                         int         num_props,
                         SmProp    **props)
{
  GsmClientXSMP *client = manager_data;
  int i;

  g_debug ("Set properties from client '%s'", client->description);

  for (i = 0; i < num_props; i++)
    {
      delete_property (client, props[i]->name);
      g_ptr_array_add (client->props, props[i]);

      debug_print_property (props[i]);

      if (!strcmp (props[i]->name, SmProgram))
	set_description (client);
    }

  free (props);

}

static void
delete_properties_callback (SmsConn     conn,
                            SmPointer   manager_data,
                            int         num_props,
                            char      **prop_names)
{
  GsmClientXSMP *client = manager_data;
  int i;

  g_debug ("Delete properties from '%s'", client->description);

  for (i = 0; i < num_props; i++)
    {
      delete_property (client, prop_names[i]);

      g_debug ("  %s", prop_names[i]);
    }

  free (prop_names);
}

static void
get_properties_callback (SmsConn   conn,
                         SmPointer manager_data)
{
  GsmClientXSMP *client = manager_data;

  g_debug ("Get properties request from '%s'", client->description);

  SmsReturnProperties (conn, client->props->len,
		       (SmProp **)client->props->pdata);
}

static const char *
xsmp_get_client_id (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;

  return xsmp->id;
}

static pid_t
xsmp_get_pid (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;
  SmProp *prop = find_property (xsmp, SmProcessID, NULL);
  char buf[32];

  if (!prop || strcmp (prop->type, SmARRAY8) != 0)
    return (pid_t)-1;

  /* prop->vals[0].value might not be '\0'-terminated... */
  g_strlcpy (buf, prop->vals[0].value, MIN (prop->vals[0].length, sizeof (buf)));
  return (pid_t)strtoul (buf, NULL, 10);
}

static char *
xsmp_get_desktop_file (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;
  SmProp *prop = find_property (xsmp, GsmDesktopFile, NULL);

  if (!prop || strcmp (prop->type, SmARRAY8) != 0)
    return NULL;

  return g_strndup (prop->vals[0].value, prop->vals[0].length);
}

static char *
prop_to_command (SmProp *prop)
{
  GString *str;
  int i, j;
  gboolean need_quotes;

  str = g_string_new (NULL);
  for (i = 0; i < prop->num_vals; i++)
    {
      char *val = prop->vals[i].value;

      need_quotes = FALSE;
      for (j = 0; j < prop->vals[i].length; j++)
	{
	  if (!g_ascii_isalnum (val[j]) && !strchr ("-_=:./", val[j]))
	    {
	      need_quotes = TRUE;
	      break;
	    }
	}

      if (i > 0)
	g_string_append_c (str, ' ');

      if (!need_quotes)
	{
	  g_string_append_printf (str, "%.*s", prop->vals[i].length,
				  (char *)prop->vals[i].value);
	}
      else
	{
	  g_string_append_c (str, '\'');
	  while (val < (char *)prop->vals[i].value + prop->vals[i].length)
	    {
	      if (*val == '\'')
		g_string_append (str, "'\''");
	      else
		g_string_append_c (str, *val);
	      val++;
	    }
	  g_string_append_c (str, '\'');
	}
    }

  return g_string_free (str, FALSE);
}

static char *
xsmp_get_restart_command (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;
  SmProp *prop = find_property (xsmp, SmRestartCommand, NULL);

  if (!prop || strcmp (prop->type, SmLISTofARRAY8) != 0)
    return NULL;

  return prop_to_command (prop);
}

static char *
xsmp_get_discard_command (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;
  SmProp *prop = find_property (xsmp, SmDiscardCommand, NULL);

  if (!prop || strcmp (prop->type, SmLISTofARRAY8) != 0)
    return NULL;

  return prop_to_command (prop);
}

static gboolean
xsmp_get_autorestart (GsmClient *client)
{
  GsmClientXSMP *xsmp = (GsmClientXSMP *) client;
  SmProp *prop = find_property (xsmp, SmRestartStyleHint, NULL);

  if (!prop || strcmp (prop->type, SmCARD8) != 0)
    return FALSE;

  return ((unsigned char *)prop->vals[0].value)[0] == SmRestartImmediately;
}

static void
set_description (GsmClientXSMP *client)
{
  SmProp *prop = find_property (client, SmProgram, NULL);

  g_free (client->description);
  if (prop)
    {
      client->description = g_strdup_printf ("%p [%.*s %s]", client,
					     prop->vals[0].length,
					     (char *)prop->vals[0].value,
					     client->id);
    }
  else if (client->id)
    client->description = g_strdup_printf ("%p [%s]", client, client->id);
  else
    client->description = g_strdup_printf ("%p", client);
}

void
gsm_client_xsmp_connect (GsmClientXSMP *client, SmsConn conn,
			 unsigned long *mask_ret, SmsCallbacks *callbacks_ret)
{
  client->conn = conn;

  if (client->protocol_timeout)
    {
      g_source_remove (client->protocol_timeout);
      client->protocol_timeout = 0;
    }

  g_debug ("Initializing client %s", client->description);

  *mask_ret = 0;

  *mask_ret |= SmsRegisterClientProcMask;
  callbacks_ret->register_client.callback = register_client_callback;
  callbacks_ret->register_client.manager_data  = client;

  *mask_ret |= SmsInteractRequestProcMask;
  callbacks_ret->interact_request.callback = interact_request_callback;
  callbacks_ret->interact_request.manager_data = client;

  *mask_ret |= SmsInteractDoneProcMask;
  callbacks_ret->interact_done.callback = interact_done_callback;
  callbacks_ret->interact_done.manager_data = client;

  *mask_ret |= SmsSaveYourselfRequestProcMask;
  callbacks_ret->save_yourself_request.callback = save_yourself_request_callback;
  callbacks_ret->save_yourself_request.manager_data = client;

  *mask_ret |= SmsSaveYourselfP2RequestProcMask;
  callbacks_ret->save_yourself_phase2_request.callback = save_yourself_phase2_request_callback;
  callbacks_ret->save_yourself_phase2_request.manager_data = client;

  *mask_ret |= SmsSaveYourselfDoneProcMask;
  callbacks_ret->save_yourself_done.callback = save_yourself_done_callback;
  callbacks_ret->save_yourself_done.manager_data = client;

  *mask_ret |= SmsCloseConnectionProcMask;
  callbacks_ret->close_connection.callback = close_connection_callback;
  callbacks_ret->close_connection.manager_data  = client;

  *mask_ret |= SmsSetPropertiesProcMask;
  callbacks_ret->set_properties.callback = set_properties_callback;
  callbacks_ret->set_properties.manager_data = client;

  *mask_ret |= SmsDeletePropertiesProcMask;
  callbacks_ret->delete_properties.callback = delete_properties_callback;
  callbacks_ret->delete_properties.manager_data = client;

  *mask_ret |= SmsGetPropertiesProcMask;
  callbacks_ret->get_properties.callback = get_properties_callback;
  callbacks_ret->get_properties.manager_data = client;
}