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
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 Novell, Inc.
 * Copyright (C) 2008 Red Hat, 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "config.h"

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

#include <gio/gio.h>
#include <glib/gi18n.h>

#include "csm-xsmp-client.h"
#include "csm-marshal.h"

#include "csm-util.h"
#include "csm-autostart-app.h"
#include "csm-manager.h"

#define CsmDesktopFile "_CSM_DesktopFile"

typedef struct {
        CsmClient  parent;
        SmsConn    conn;
        IceConn    ice_connection;

        guint      watch_id;

        char      *description;
        GPtrArray *props;

        /* SaveYourself state */
        int        current_save_yourself;
        int        next_save_yourself;
        guint      next_save_yourself_allow_interact : 1;
} CsmXSMPClientPrivate;

enum {
        PROP_0,
        PROP_ICE_CONNECTION
};

enum {
        REGISTER_REQUEST,
        LOGOUT_REQUEST,
        LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE_WITH_PRIVATE (CsmXSMPClient, csm_xsmp_client, CSM_TYPE_CLIENT)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.

static gboolean
client_iochannel_watch (GIOChannel    *channel,
                        GIOCondition   condition,
                        CsmXSMPClient *client)
{
        gboolean keep_going;
        CsmXSMPClientPrivate *priv;

        g_object_ref (client);
        priv = csm_xsmp_client_get_instance_private (client);

        switch (IceProcessMessages (priv->ice_connection, NULL, NULL)) {
        case IceProcessMessagesSuccess:
                keep_going = TRUE;
                break;

        case IceProcessMessagesIOError:
                g_debug ("CsmXSMPClient: IceProcessMessagesIOError on '%s'", priv->description);
                csm_client_set_status (CSM_CLIENT (client), CSM_CLIENT_FAILED);
                /* Emitting "disconnected" will eventually cause
                 * IceCloseConnection() to be called.
                 */
                csm_client_disconnected (CSM_CLIENT (client));
                keep_going = FALSE;
                break;

        case IceProcessMessagesConnectionClosed:
                g_debug ("CsmXSMPClient: IceProcessMessagesConnectionClosed on '%s'",
                         priv->description);
                priv->ice_connection = NULL;
                keep_going = FALSE;
                break;

        default:
                g_assert_not_reached ();
        }
        g_object_unref (client);

        return keep_going;
}

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

        priv = csm_xsmp_client_get_instance_private (client);

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

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

        return NULL;
}

static void
set_description (CsmXSMPClient *client)
{
        SmProp     *prop;
        const char *id;
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (client);
        prop = find_property (client, SmProgram, NULL);
        id = csm_client_peek_startup_id (CSM_CLIENT (client));

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

static void
setup_connection (CsmXSMPClient *client)
{
        GIOChannel    *channel;
        int            fd;
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (client);
        g_debug ("CsmXSMPClient: Setting up new connection");

        fd = IceConnectionNumber (priv->ice_connection);
        fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC);
        channel = g_io_channel_unix_new (fd);
        priv->watch_id = g_io_add_watch (channel,
                                         G_IO_IN | G_IO_ERR,
                                         (GIOFunc)client_iochannel_watch,
                                         client);
        g_io_channel_unref (channel);

        set_description (client);

        g_debug ("CsmXSMPClient: New client '%s'", priv->description);
}

static GObject *
csm_xsmp_client_constructor (GType                  type,
                             guint                  n_construct_properties,
                             GObjectConstructParam *construct_properties)
{
        CsmXSMPClient *client;

        client = CSM_XSMP_CLIENT (G_OBJECT_CLASS (csm_xsmp_client_parent_class)->constructor (type,
                                                                                              n_construct_properties,
                                                                                              construct_properties));
        setup_connection (client);

        return G_OBJECT (client);
}

static void
csm_xsmp_client_init (CsmXSMPClient *client)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (client);

        priv->props = g_ptr_array_new ();
        priv->current_save_yourself = -1;
        priv->next_save_yourself = -1;
        priv->next_save_yourself_allow_interact = FALSE;
}


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

        priv = csm_xsmp_client_get_instance_private (client);

        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 CsmAppResumed,
         * 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)) {
                csm_client_run_discard (CSM_CLIENT (client));
        }
#endif

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


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

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

        case 'A': /* ARRAY8 */
                g_debug ("CsmXSMPClient:   %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 ("CsmXSMPClient:   %s = %s", prop->name, tmp->str);
                g_string_free (tmp, TRUE);
                break;

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


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

        priv = csm_xsmp_client_get_instance_private (client);

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

        for (i = 0; i < num_props; i++) {
                delete_property (client, props[i]->name);
                g_ptr_array_add (priv->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)
{
        int i;
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);

        g_debug ("CsmXSMPClient: Delete properties from '%s'", priv->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)
{
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);

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

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

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 (CsmClient *client)
{
        SmProp *prop;

        prop = find_property (CSM_XSMP_CLIENT (client), SmRestartCommand, NULL);

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

        return prop_to_command (prop);
}

static char *
xsmp_get_discard_command (CsmClient *client)
{
        SmProp *prop;

        prop = find_property (CSM_XSMP_CLIENT (client), SmDiscardCommand, NULL);

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

        return prop_to_command (prop);
}

static void
do_save_yourself (CsmXSMPClient *client,
                  int            save_type,
                  gboolean       allow_interact)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (client);
        g_assert (priv->conn != NULL);

        if (priv->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 ("CsmXSMPClient:   skipping redundant SaveYourself for '%s'",
                         priv->description);
        } else if (priv->current_save_yourself != -1) {
                g_debug ("CsmXSMPClient:   queuing new SaveYourself for '%s'",
                         priv->description);
                priv->next_save_yourself = save_type;
                priv->next_save_yourself_allow_interact = allow_interact;
        } else {
                priv->current_save_yourself = save_type;
                /* make sure we don't have anything queued */
                priv->next_save_yourself = -1;
                priv->next_save_yourself_allow_interact = FALSE;

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

                default:
                        /* Logout */
                        if (!allow_interact) {
                                SmsSaveYourself (priv->conn,
                                                 save_type, /* save type */
                                                 TRUE, /* shutdown */
                                                 SmInteractStyleNone, /* interact style */
                                                 TRUE); /* fast */
                        } else {
                                SmsSaveYourself (priv->conn,
                                                 save_type, /* save type */
                                                 TRUE, /* shutdown */
                                                 SmInteractStyleAny, /* interact style */
                                                 FALSE /* fast */);
                        }
                        break;
                }
        }
}

static void
xsmp_save_yourself_phase2 (CsmClient *client)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (CSM_XSMP_CLIENT(client));

        g_debug ("CsmXSMPClient: xsmp_save_yourself_phase2 ('%s')", priv->description);

        SmsSaveYourselfPhase2 (priv->conn);
}

static void
xsmp_interact (CsmClient *client)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (CSM_XSMP_CLIENT(client));

        g_debug ("CsmXSMPClient: xsmp_interact ('%s')", priv->description);

        SmsInteract (priv->conn);
}

static gboolean
xsmp_cancel_end_session (CsmClient *client,
                         GError   **error)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (CSM_XSMP_CLIENT(client));

        g_debug ("CsmXSMPClient: xsmp_cancel_end_session ('%s')", priv->description);

        if (priv->conn == NULL) {
                g_set_error (error,
                             CSM_CLIENT_ERROR,
                             CSM_CLIENT_ERROR_NOT_REGISTERED,
                             "Client is not registered");
                return FALSE;
        }

        SmsShutdownCancelled (priv->conn);

        /* reset the state */
        priv->current_save_yourself = -1;
        priv->next_save_yourself = -1;
        priv->next_save_yourself_allow_interact = FALSE;

        return TRUE;
}

static char *
get_desktop_file_path (CsmXSMPClient *client)
{
        SmProp     *prop;
        char       *desktop_file_path = NULL;
        char      **dirs;
        const char *program_name;

        /* XSMP clients using eggsmclient defines a special property
         * pointing to their respective desktop entry file */
        prop = find_property (client, CsmDesktopFile, NULL);

        if (prop) {
                GFile *file = g_file_new_for_uri (prop->vals[0].value);
                desktop_file_path = g_file_get_path (file);
                g_object_unref (file);
                goto out;
        }

        /* If we can't get desktop file from CsmDesktopFile then we
         * try to find the desktop file from its program name */
        prop = find_property (client, SmProgram, NULL);

        if (!prop) {
                goto out;
        }

        program_name = prop->vals[0].value;

        dirs = csm_util_get_autostart_dirs ();

        desktop_file_path =
                csm_util_find_desktop_file_for_app_name (program_name,
                                                         dirs);

        g_strfreev (dirs);

out:
        g_debug ("CsmXSMPClient: desktop file for client %s is %s",
                 csm_client_peek_id (CSM_CLIENT (client)),
                 desktop_file_path ? desktop_file_path : "(null)");

        return desktop_file_path;
}

static void
set_desktop_file_keys_from_client (CsmClient *client,
                                   GKeyFile  *keyfile)
{
        SmProp       *prop;
        const char   *name;
        char         *comment;

        prop = find_property (CSM_XSMP_CLIENT (client), SmProgram, NULL);

        if (prop) {
                name = prop->vals[0].value;
        } else {
            /* It'd be really surprising to reach this code: if we're here,
             * then the XSMP client already has set several XSMP
             * properties. But it could still be that SmProgram is not set.
             */
            name = _("Remembered Application");
        }

        comment = g_strdup_printf ("Client %s which was automatically saved",
                                   csm_client_peek_startup_id (client));

        g_key_file_set_string (keyfile,
                               G_KEY_FILE_DESKTOP_GROUP,
                               G_KEY_FILE_DESKTOP_KEY_NAME,
                               name);

        g_key_file_set_string (keyfile,
                               G_KEY_FILE_DESKTOP_GROUP,
                               G_KEY_FILE_DESKTOP_KEY_COMMENT,
                               comment);

        g_key_file_set_string (keyfile,
                               G_KEY_FILE_DESKTOP_GROUP,
                               G_KEY_FILE_DESKTOP_KEY_ICON,
                               "system-run");

        g_key_file_set_string (keyfile,
                               G_KEY_FILE_DESKTOP_GROUP,
                               G_KEY_FILE_DESKTOP_KEY_TYPE,
                               "Application");

        g_key_file_set_boolean (keyfile,
                                G_KEY_FILE_DESKTOP_GROUP,
                                G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY,
                                TRUE);

        g_free (comment);
}

static GKeyFile *
create_client_key_file (CsmClient   *client,
                        const char  *desktop_file_path,
                        GError     **error) {
        GKeyFile *keyfile;

        keyfile = g_key_file_new ();

        if (desktop_file_path != NULL) {
                g_key_file_load_from_file (keyfile,
                                           desktop_file_path,
                                           G_KEY_FILE_KEEP_COMMENTS |
                                           G_KEY_FILE_KEEP_TRANSLATIONS,
                                           error);
        } else {
                set_desktop_file_keys_from_client (client, keyfile);
        }

        return keyfile;
}

static CsmClientRestartStyle
xsmp_get_restart_style_hint (CsmClient *client);

static GKeyFile *
xsmp_save (CsmClient *client,
           GError   **error)
{
        CsmClientRestartStyle restart_style;

        GKeyFile *keyfile = NULL;
        char     *desktop_file_path = NULL;
        char     *exec_program = NULL;
        char     *exec_discard = NULL;
        char     *startup_id = NULL;
        GError   *local_error;

        g_debug ("CsmXSMPClient: saving client with id %s",
                 csm_client_peek_id (client));

        local_error = NULL;

        restart_style = xsmp_get_restart_style_hint (client);
        if (restart_style == CSM_CLIENT_RESTART_NEVER) {
                goto out;
        }

        exec_program = xsmp_get_restart_command (client);
        if (!exec_program) {
                goto out;
        }

        desktop_file_path = get_desktop_file_path (CSM_XSMP_CLIENT (client));

        /* this can accept desktop_file_path == NULL */
        keyfile = create_client_key_file (client,
                                          desktop_file_path,
                                          &local_error);

        if (local_error) {
                goto out;
        }

        g_object_get (client,
                      "startup-id", &startup_id,
                      NULL);

        g_key_file_set_string (keyfile,
                               G_KEY_FILE_DESKTOP_GROUP,
                               CSM_AUTOSTART_APP_STARTUP_ID_KEY,
                               startup_id);

        g_key_file_set_string (keyfile,
                               G_KEY_FILE_DESKTOP_GROUP,
                               G_KEY_FILE_DESKTOP_KEY_EXEC,
                               exec_program);

        exec_discard = xsmp_get_discard_command (client);
        if (exec_discard)
                g_key_file_set_string (keyfile,
                                       G_KEY_FILE_DESKTOP_GROUP,
                                       CSM_AUTOSTART_APP_DISCARD_KEY,
                                       exec_discard);

out:
        g_free (desktop_file_path);
        g_free (exec_program);
        g_free (exec_discard);
        g_free (startup_id);

        if (local_error != NULL) {
                g_propagate_error (error, local_error);
                g_key_file_free (keyfile);

                return NULL;
        }

        return keyfile;
}

static gboolean
xsmp_stop (CsmClient *client,
           GError   **error)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (CSM_XSMP_CLIENT(client));

        g_debug ("CsmXSMPClient: xsmp_stop ('%s')", priv->description);

        if (priv->conn == NULL) {
                g_set_error (error,
                             CSM_CLIENT_ERROR,
                             CSM_CLIENT_ERROR_NOT_REGISTERED,
                             "Client is not registered");
                return FALSE;
        }

        SmsDie (priv->conn);

        return TRUE;
}

static gboolean
xsmp_query_end_session (CsmClient *client,
                        guint      flags,
                        GError   **error)
{
        gboolean allow_interact;
        int      save_type;
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (CSM_XSMP_CLIENT(client));

        if (priv->conn == NULL) {
                g_set_error (error,
                             CSM_CLIENT_ERROR,
                             CSM_CLIENT_ERROR_NOT_REGISTERED,
                             "Client is not registered");
                return FALSE;
        }

        allow_interact = !(flags & CSM_CLIENT_END_SESSION_FLAG_FORCEFUL);

        /* we don't want to save the session state, but we just want to know if
         * there's user data the client has to save and we want to give the
         * client a chance to tell the user about it. This is consistent with
         * the manager not setting CSM_CLIENT_END_SESSION_FLAG_SAVE for this
         * phase. */
        save_type = SmSaveGlobal;

        do_save_yourself (CSM_XSMP_CLIENT (client), save_type, allow_interact);
        return TRUE;
}

static gboolean
xsmp_end_session (CsmClient *client,
                  guint      flags,
                  GError   **error)
{
        gboolean phase2;
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (CSM_XSMP_CLIENT(client));

        if (priv->conn == NULL) {
                g_set_error (error,
                             CSM_CLIENT_ERROR,
                             CSM_CLIENT_ERROR_NOT_REGISTERED,
                             "Client is not registered");
                return FALSE;
        }

        phase2 = (flags & CSM_CLIENT_END_SESSION_FLAG_LAST);

        if (phase2) {
                xsmp_save_yourself_phase2 (client);
        } else {
                gboolean allow_interact;
                int      save_type;

                /* we gave a chance to interact to the app during
                 * xsmp_query_end_session(), now it's too late to interact */
                allow_interact = FALSE;

                if (flags & CSM_CLIENT_END_SESSION_FLAG_SAVE) {
                        save_type = SmSaveBoth;
                } else {
                        save_type = SmSaveGlobal;
                }

                do_save_yourself (CSM_XSMP_CLIENT (client),
                                  save_type, allow_interact);
        }

        return TRUE;
}

static char *
xsmp_get_app_name (CsmClient *client)
{
        SmProp *prop;
        char   *name = NULL;

        prop = find_property (CSM_XSMP_CLIENT (client), SmProgram, NULL);
        if (prop) {
                name = prop_to_command (prop);
        }

        return name;
}

static void
csm_client_set_ice_connection (CsmXSMPClient *client,
                               gpointer       conn)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (client);
        priv->ice_connection = conn;
}

static void
csm_xsmp_client_set_property (GObject       *object,
                              guint          prop_id,
                              const GValue  *value,
                              GParamSpec    *pspec)
{
        CsmXSMPClient *self;

        self = CSM_XSMP_CLIENT (object);

        switch (prop_id) {
        case PROP_ICE_CONNECTION:
                csm_client_set_ice_connection (self, g_value_get_pointer (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
csm_xsmp_client_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (CSM_XSMP_CLIENT(object));

        switch (prop_id) {
        case PROP_ICE_CONNECTION:
                g_value_set_pointer (value, priv->ice_connection);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
csm_xsmp_client_disconnect (CsmXSMPClient *client)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (client);
        if (priv->watch_id > 0) {
                g_source_remove (priv->watch_id);
        }

        if (priv->conn != NULL) {
                SmsCleanUp (priv->conn);
        }

        if (priv->ice_connection != NULL) {
                IceSetShutdownNegotiation (priv->ice_connection, FALSE);
                IceCloseConnection (priv->ice_connection);
        }
}

static void
csm_xsmp_client_finalize (GObject *object)
{
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client;

        client = CSM_XSMP_CLIENT(object);

        priv = csm_xsmp_client_get_instance_private (client);

        g_debug ("CsmXSMPClient: xsmp_finalize (%s)", priv->description);
        csm_xsmp_client_disconnect (client);

        g_free (priv->description);
        g_ptr_array_foreach (priv->props, (GFunc)SmFreeProperty, NULL);
        g_ptr_array_free (priv->props, TRUE);

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

static gboolean
_boolean_handled_accumulator (GSignalInvocationHint *ihint,
                              GValue                *return_accu,
                              const GValue          *handler_return,
                              gpointer               dummy)
{
        gboolean    continue_emission;
        gboolean    signal_handled;

        signal_handled = g_value_get_boolean (handler_return);
        g_value_set_boolean (return_accu, signal_handled);
        continue_emission = !signal_handled;

        return continue_emission;
}

static CsmClientRestartStyle
xsmp_get_restart_style_hint (CsmClient *client)
{
        SmProp               *prop;
        CsmClientRestartStyle hint;

        g_debug ("CsmXSMPClient: getting restart style");
        hint = CSM_CLIENT_RESTART_IF_RUNNING;

        prop = find_property (CSM_XSMP_CLIENT (client), SmRestartStyleHint, NULL);

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

        switch (((unsigned char *)prop->vals[0].value)[0]) {
        case SmRestartIfRunning:
                hint = CSM_CLIENT_RESTART_IF_RUNNING;
                break;
        case SmRestartAnyway:
                hint = CSM_CLIENT_RESTART_ANYWAY;
                break;
        case SmRestartImmediately:
                hint = CSM_CLIENT_RESTART_IMMEDIATELY;
                break;
        case SmRestartNever:
                hint = CSM_CLIENT_RESTART_NEVER;
                break;
        default:
                break;
        }

        return hint;
}

static gboolean
_parse_value_as_uint (const char *value,
                      guint      *uintval)
{
        char  *end_of_valid_uint;
        gulong ulong_value;
        guint  uint_value;

        errno = 0;
        ulong_value = strtoul (value, &end_of_valid_uint, 10);

        if (*value == '\0' || *end_of_valid_uint != '\0') {
                return FALSE;
        }

        uint_value = ulong_value;
        if (uint_value != ulong_value || errno == ERANGE) {
                return FALSE;
        }

        *uintval = uint_value;

        return TRUE;
}

static guint
xsmp_get_unix_process_id (CsmClient *client)
{
        SmProp  *prop;
        guint    pid;
        gboolean res;

        g_debug ("CsmXSMPClient: getting pid");

        prop = find_property (CSM_XSMP_CLIENT (client), SmProcessID, NULL);

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

        pid = 0;
        res = _parse_value_as_uint ((char *)prop->vals[0].value, &pid);
        if (! res) {
                pid = 0;
        }

        return pid;
}

static void
csm_xsmp_client_class_init (CsmXSMPClientClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
        CsmClientClass *client_class = CSM_CLIENT_CLASS (klass);

        object_class->finalize             = csm_xsmp_client_finalize;
        object_class->constructor          = csm_xsmp_client_constructor;
        object_class->get_property         = csm_xsmp_client_get_property;
        object_class->set_property         = csm_xsmp_client_set_property;

        client_class->impl_save                   = xsmp_save;
        client_class->impl_stop                   = xsmp_stop;
        client_class->impl_query_end_session      = xsmp_query_end_session;
        client_class->impl_end_session            = xsmp_end_session;
        client_class->impl_cancel_end_session     = xsmp_cancel_end_session;
        client_class->impl_get_app_name           = xsmp_get_app_name;
        client_class->impl_get_restart_style_hint = xsmp_get_restart_style_hint;
        client_class->impl_get_unix_process_id    = xsmp_get_unix_process_id;

        signals[REGISTER_REQUEST] =
                g_signal_new ("register-request",
                              G_OBJECT_CLASS_TYPE (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (CsmXSMPClientClass, register_request),
                              _boolean_handled_accumulator,
                              NULL,
                              csm_marshal_BOOLEAN__POINTER,
                              G_TYPE_BOOLEAN,
                              1, G_TYPE_POINTER);
        signals[LOGOUT_REQUEST] =
                g_signal_new ("logout-request",
                              G_OBJECT_CLASS_TYPE (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (CsmXSMPClientClass, logout_request),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__BOOLEAN,
                              G_TYPE_NONE,
                              1, G_TYPE_BOOLEAN);

        g_object_class_install_property (object_class,
                                         PROP_ICE_CONNECTION,
                                         g_param_spec_pointer ("ice-connection",
                                                               "ice-connection",
                                                               "ice-connection",
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

CsmClient *
csm_xsmp_client_new (IceConn ice_conn)
{
        CsmXSMPClient *xsmp;

        xsmp = g_object_new (CSM_TYPE_XSMP_CLIENT,
                             "ice-connection", ice_conn,
                             NULL);

        return CSM_CLIENT (xsmp);
}

static Status
register_client_callback (SmsConn    conn,
                          SmPointer  manager_data,
                          char      *previous_id)
{
        gboolean       handled;
        char          *id;
        CsmXSMPClientPrivate *priv;

        CsmXSMPClient *client = manager_data;
        priv = csm_xsmp_client_get_instance_private (client);

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


        /* There are three cases:
         * 1. id is NULL - we'll use a new one
         * 2. id is known - we'll use known one
         * 3. id is unknown - this is an error
         */
        id = g_strdup (previous_id);

        handled = FALSE;
        g_signal_emit (client, signals[REGISTER_REQUEST], 0, &id, &handled);
        if (! handled) {
                g_debug ("CsmXSMPClient:  RegisterClient not handled!");
                g_free (id);
                free (previous_id);
                g_assert_not_reached ();
                return FALSE;
        }

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

        g_object_set (client, "startup-id", id, NULL);

        set_description (client);

        g_debug ("CsmXSMPClient: Sending RegisterClientReply to '%s'", priv->description);

        SmsRegisterClientReply (conn, id);

        if (IS_STRING_EMPTY (previous_id)) {
                /* Send the initial SaveYourself. */
                g_debug ("CsmXSMPClient: Sending initial SaveYourself");
                SmsSaveYourself (conn, SmSaveLocal, False, SmInteractStyleNone, False);
                priv->current_save_yourself = SmSaveLocal;
        }

        csm_client_set_status (CSM_CLIENT (client), CSM_CLIENT_REGISTERED);

        g_free (id);
        free (previous_id);

        return TRUE;
}


static void
save_yourself_request_callback (SmsConn   conn,
                                SmPointer manager_data,
                                int       save_type,
                                Bool      shutdown,
                                int       interact_style,
                                Bool      fast,
                                Bool      global)
{
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);

        g_debug ("CsmXSMPClient: Client '%s' received SaveYourselfRequest(%s, %s, %s, %s, %s)",
                 priv->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 cafe-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 ("CsmXSMPClient:   initiating shutdown");
                g_signal_emit (client, signals[LOGOUT_REQUEST], 0, !fast);
        } else if (!shutdown && !global) {
                g_debug ("CsmXSMPClient:   initiating checkpoint");
                do_save_yourself (client, SmSaveLocal, TRUE);
        } else {
                g_debug ("CsmXSMPClient:   ignoring");
        }
}

static void
save_yourself_phase2_request_callback (SmsConn   conn,
                                       SmPointer manager_data)
{
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);

        g_debug ("CsmXSMPClient: Client '%s' received SaveYourselfPhase2Request",
                 priv->description);

        priv->current_save_yourself = -1;

        /* this is a valid response to SaveYourself and therefore
           may be a response to a QES or ES */
        csm_client_end_session_response (CSM_CLIENT (client),
                                         TRUE, TRUE, FALSE,
                                         NULL);
}

static void
interact_request_callback (SmsConn   conn,
                           SmPointer manager_data,
                           int       dialog_type)
{
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);
#if 0
        gboolean       res;
        GError        *error;
#endif

        g_debug ("CsmXSMPClient: Client '%s' received InteractRequest(%s)",
                 priv->description,
                 dialog_type == SmDialogNormal ? "Dialog" : "Errors");

        csm_client_end_session_response (CSM_CLIENT (client),
                                         FALSE, FALSE, FALSE,
                                         _("This program is blocking logout."));

#if 0
        /* Can't just call back with Interact because session client
           grabs the keyboard!  So, we try to get it to release
           grabs by telling it we've cancelled the shutdown.
           This grabbing is clearly bullshit and is not supported by
           the client spec or protocol spec.
        */
        res = xsmp_cancel_end_session (CSM_CLIENT (client), &error);
        if (! res) {
                g_warning ("Unable to cancel end session: %s", error->message);
                g_error_free (error);
        }
#endif
        xsmp_interact (CSM_CLIENT (client));
}

static void
interact_done_callback (SmsConn   conn,
                        SmPointer manager_data,
                        Bool      cancel_shutdown)
{
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);

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

        csm_client_end_session_response (CSM_CLIENT (client),
                                         TRUE, FALSE, cancel_shutdown,
                                         NULL);
}

static void
save_yourself_done_callback (SmsConn   conn,
                             SmPointer manager_data,
                             Bool      success)
{
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);

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

        if (priv->current_save_yourself != -1) {
                SmsSaveComplete (priv->conn);
                priv->current_save_yourself = -1;
        }

        /* If success is false then the application couldn't save data. Nothing
         * the session manager can do about, though. FIXME: we could display a
         * dialog about this, I guess. */
        csm_client_end_session_response (CSM_CLIENT (client),
                                         TRUE, FALSE, FALSE,
                                         NULL);

        if (priv->next_save_yourself) {
                int      save_type = priv->next_save_yourself;
                gboolean allow_interact = priv->next_save_yourself_allow_interact;

                priv->next_save_yourself = -1;
                priv->next_save_yourself_allow_interact = -1;
                do_save_yourself (client, save_type, allow_interact);
        }
}

static void
close_connection_callback (SmsConn     conn,
                           SmPointer   manager_data,
                           int         count,
                           char      **reason_msgs)
{
        int            i;
        CsmXSMPClientPrivate *priv;
        CsmXSMPClient *client = manager_data;

        priv = csm_xsmp_client_get_instance_private (client);

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

        csm_client_set_status (CSM_CLIENT (client), CSM_CLIENT_FINISHED);
        csm_client_disconnected (CSM_CLIENT (client));
}

void
csm_xsmp_client_connect (CsmXSMPClient *client,
                         SmsConn        conn,
                         unsigned long *mask_ret,
                         SmsCallbacks  *callbacks_ret)
{
        CsmXSMPClientPrivate *priv;

        priv = csm_xsmp_client_get_instance_private (client);
        priv->conn = conn;

        g_debug ("CsmXSMPClient: Initializing client %s", priv->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;
}

void
csm_xsmp_client_save_state (CsmXSMPClient *client)
{
        g_return_if_fail (CSM_IS_XSMP_CLIENT (client));
}