Codebase list policykit-1-gnome / debian/0.8-1 polkit-gnome / polkit-gnome-action.c
debian/0.8-1

Tree @debian/0.8-1 (Download .tar.gz)

polkit-gnome-action.c @debian/0.8-1raw · history · 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
 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
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/***************************************************************************
 *
 * polkit-gnome-action.c : 
 *
 * Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This library 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 Lesser General Public
 * License along with this library; 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>

#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>

#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include <polkit/polkit.h>
#include <polkit-dbus/polkit-dbus.h>

#include "polkit-gnome-context.h"
#include "polkit-gnome-action.h"
#include "polkit-gnome-auth.h"

/**
 * SECTION:polkit-gnome-action
 * @short_description: A GtkAction subclass where instances can be tied to a specific PolicyKit action.
 *
 * Actions in GTK+ represent operations that the user can be perform,
 * along with some information how it should be presented in the
 * interface. Each action provides methods to create icons, menu items
 * and toolbar items representing itself. Each action can have one or
 * more proxy menu item, toolbar button or other proxy
 * widgets. Proxies mirror the state of the action (text label,
 * tooltip, icon, visible, sensitive, etc), and should change when the
 * action's state changes. When the proxy is activated, it should
 * activate its action.
 *
 * Instances of #PolKitGnomeAction class updates the label, tooltip,
 * icon-name, visible and properties of the parent #GtkAction instance
 * according to what result PolicyKit gives about a given
 * #PolKitAction object. The #PolKitGnomeContext class is used
 * internally to track changes. This means that external events (such
 * as the editing of the /etc/PolicyKit/PolicyKit.conf file,
 * ConsoleKit session activity changes or if the user gains a
 * privilege via authentication) will trigger the action, and thus
 * connected proxy widgets, to be updated.
 *
 * In addition, the #PolKitGnomeAction class intercepts the ::activate
 * signal defined in #GtkAction. When the result from PolicyKit is
 * yes, the signal is propagated. If the result is auth,
 * polkit_gnome_auth_show_dialog() will be used to bring up an
 * authentication dialog for the given #PolKitAction. If the user
 * succesfully gained the privilege, a ::activate signal will be
 * synthesized. If the result is no, the signal is also propagated.
 *
 * As a result, everything happens under the covers; the application
 * programmer using #PolKitGnomeAction will only get the ::activate
 * signal when the answer from PolicyKit is yes and as such don't have
 * to worry about bringing up authentication dialogs if the property
 * "no-sensitive" is set to #FALSE.
 *
 * When an authentication dialog is show, the #PolKitGnomeAction class
 * will pass the XID of the top-level window that the proxy widget
 * causing the activation to polkit_gnome_auth_show_dialog().
 *
 * An example of how to use #PolKitGnomeAction follows. First, build
 * the following program
 * 
 * <programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="../../examples/polkit-gnome-example.c" parse="text"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting>
 *
 * with
 *
 * <programlisting>gcc -o polkit-gnome-example `pkg-config --cflags --libs polkit-gnome` polkit-gnome-example.c</programlisting>
 *
 * Then, put the following content
 *
 * <programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="../../examples/polkit-gnome-example.policy" parse="text"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting>
 *
 * into a file
 * <literal>/usr/share/PolicyKit/policy/polkit-gnome-example.policy</literal>. Finally,
 * run <literal>polkit-gnome-example</literal>. It should display a
 * window like this:
 *
 * <inlinegraphic fileref="polkit-gnome-example-screenshot.png" format="PNG"/>
 *
 * If the "Twiddle!" button is pressed, an authentication dialog
 * should pop up
 *
 * <inlinegraphic fileref="polkit-gnome-example-auth-dialog-twiddle.png" format="PNG"/>
 *
 * Here is how what the application looks like if the user gains
 * authorization for all the actions:
 *
 * <inlinegraphic fileref="polkit-gnome-example-screenshot-authorized.png" format="PNG"/>
 *
 * Regarding how to build an example mechanism that this GTK+
 * application can take advantage of, please refer to the
 * #PolKitContext class for examples.
 **/

#define POLKIT_GNOME_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), POLKIT_GNOME_TYPE_ACTION, PolKitGnomeActionPrivate))

static void polkit_gnome_action_activate (PolKitGnomeAction *action);
static void _auth_start (PolKitGnomeAction *action);

static void polkit_gnome_action_set_polkit_action         (PolKitGnomeAction *action, PolKitAction *pk_action);
static void polkit_gnome_action_set_polkit_action_sufficient (PolKitGnomeAction *action, const GValue *pk_action_array);

static void polkit_gnome_action_set_self_blocked_visible     (PolKitGnomeAction *action, gboolean visible);
static void polkit_gnome_action_set_self_blocked_sensitive   (PolKitGnomeAction *action, gboolean sensitive);
static void polkit_gnome_action_set_self_blocked_short_label (PolKitGnomeAction *action, const gchar *short_label);
static void polkit_gnome_action_set_self_blocked_label       (PolKitGnomeAction *action, const gchar *label);
static void polkit_gnome_action_set_self_blocked_tooltip     (PolKitGnomeAction *action, const gchar *tooltip);
static void polkit_gnome_action_set_self_blocked_icon_name   (PolKitGnomeAction *action, const gchar *icon_name);

static void polkit_gnome_action_set_no_visible               (PolKitGnomeAction *action, gboolean visible);
static void polkit_gnome_action_set_no_sensitive             (PolKitGnomeAction *action, gboolean sensitive);
static void polkit_gnome_action_set_no_short_label           (PolKitGnomeAction *action, const gchar *short_label);
static void polkit_gnome_action_set_no_label                 (PolKitGnomeAction *action, const gchar *label);
static void polkit_gnome_action_set_no_tooltip               (PolKitGnomeAction *action, const gchar *tooltip);
static void polkit_gnome_action_set_no_icon_name             (PolKitGnomeAction *action, const gchar *icon_name);

static void polkit_gnome_action_set_auth_visible             (PolKitGnomeAction *action, gboolean visible);
static void polkit_gnome_action_set_auth_sensitive           (PolKitGnomeAction *action, gboolean sensitive);
static void polkit_gnome_action_set_auth_short_label         (PolKitGnomeAction *action, const gchar *short_label);
static void polkit_gnome_action_set_auth_label               (PolKitGnomeAction *action, const gchar *label);
static void polkit_gnome_action_set_auth_tooltip             (PolKitGnomeAction *action, const gchar *tooltip);
static void polkit_gnome_action_set_auth_icon_name           (PolKitGnomeAction *action, const gchar *icon_name);

static void polkit_gnome_action_set_yes_visible              (PolKitGnomeAction *action, gboolean visible);
static void polkit_gnome_action_set_yes_sensitive            (PolKitGnomeAction *action, gboolean sensitive);
static void polkit_gnome_action_set_yes_short_label          (PolKitGnomeAction *action, const gchar *short_label);
static void polkit_gnome_action_set_yes_label                (PolKitGnomeAction *action, const gchar *label);
static void polkit_gnome_action_set_yes_tooltip              (PolKitGnomeAction *action, const gchar *tooltip);
static void polkit_gnome_action_set_yes_icon_name            (PolKitGnomeAction *action, const gchar *icon_name);

static void polkit_gnome_action_set_target_pid               (PolKitGnomeAction *action, guint target_pid);

static void _pk_config_changed (PolKitGnomeContext *pk_g_context, PolKitGnomeAction  *action);
static void _pk_console_kit_db_changed (PolKitGnomeContext *pk_g_context, PolKitGnomeAction  *action);

struct _PolKitGnomeActionPrivate 
{
        gboolean self_blocked_visible;
        gboolean self_blocked_sensitive;
        gchar *self_blocked_short_label;
        gchar *self_blocked_label;
        gchar *self_blocked_tooltip;
        gchar *self_blocked_icon_name;

        gboolean no_visible;
        gboolean no_sensitive;
        gchar *no_short_label;
        gchar *no_label;
        gchar *no_tooltip;
        gchar *no_icon_name;

        gboolean auth_visible;
        gboolean auth_sensitive;
        gchar *auth_short_label;
        gchar *auth_label;
        gchar *auth_tooltip;
        gchar *auth_icon_name;

        gboolean yes_visible;
        gboolean yes_sensitive;
        gchar *yes_short_label;
        gchar *yes_label;
        gchar *yes_tooltip;
        gchar *yes_icon_name;

        gboolean master_visible;
        gboolean master_sensitive;

        PolKitAction *polkit_action;
        GSList *polkit_action_sufficient;

        gboolean polkit_action_set_once;

        guint target_pid;

        /* the current PolicyKit result for the given polkit_action_id */
        PolKitResult pk_result;

        PolKitGnomeContext *pk_g_context;

        gulong config_changed_handler_id;
        gulong console_kit_db_changed_handler_id;
};

enum 
{
        AUTH_START_SIGNAL,
        AUTH_END_SIGNAL,
        POLKIT_RESULT_CHANGED_SIGNAL,
        LAST_SIGNAL
};

enum
{
        PROP_0,
        PROP_POLKIT_ACTION_OBJ,
        PROP_POLKIT_ACTION_OBJ_SUFFICIENT,

        PROP_POLKIT_SELF_BLOCKED_VISIBLE,
        PROP_POLKIT_SELF_BLOCKED_SENSITIVE,
        PROP_POLKIT_SELF_BLOCKED_SHORT_LABEL,
        PROP_POLKIT_SELF_BLOCKED_LABEL,
        PROP_POLKIT_SELF_BLOCKED_TOOLTIP,
        PROP_POLKIT_SELF_BLOCKED_ICON_NAME,

        PROP_POLKIT_NO_VISIBLE,
        PROP_POLKIT_NO_SENSITIVE,
        PROP_POLKIT_NO_SHORT_LABEL,
        PROP_POLKIT_NO_LABEL,
        PROP_POLKIT_NO_TOOLTIP,
        PROP_POLKIT_NO_ICON_NAME,

        PROP_POLKIT_AUTH_VISIBLE,
        PROP_POLKIT_AUTH_SENSITIVE,
        PROP_POLKIT_AUTH_SHORT_LABEL,
        PROP_POLKIT_AUTH_LABEL,
        PROP_POLKIT_AUTH_TOOLTIP,
        PROP_POLKIT_AUTH_ICON_NAME,

        PROP_POLKIT_YES_VISIBLE,
        PROP_POLKIT_YES_SENSITIVE,
        PROP_POLKIT_YES_SHORT_LABEL,
        PROP_POLKIT_YES_LABEL,
        PROP_POLKIT_YES_TOOLTIP,
        PROP_POLKIT_YES_ICON_NAME,

        PROP_POLKIT_MASTER_VISIBLE,
        PROP_POLKIT_MASTER_SENSITIVE,

        PROP_POLKIT_TARGET_PID,
};

G_DEFINE_TYPE (PolKitGnomeAction, polkit_gnome_action, GTK_TYPE_ACTION)

static void set_property                   (GObject         *object,
					    guint            prop_id,
					    const GValue    *value,
					    GParamSpec      *pspec);
static void get_property                   (GObject         *object,
					    guint            prop_id,
					    GValue          *value,
					    GParamSpec      *pspec);

static GObjectClass *parent_class = NULL;
static guint         signals[LAST_SIGNAL] = { 0 };

static void
polkit_gnome_action_init (PolKitGnomeAction *action)
{
        action->priv = POLKIT_GNOME_ACTION_GET_PRIVATE (action);


}


static void
free_pk_action_sufficient (PolKitGnomeAction *action)
{
        if (action->priv->polkit_action_sufficient != NULL) {
                GSList *l;

                for (l = action->priv->polkit_action_sufficient; l != NULL; l = g_slist_next (l)) {
                        polkit_action_unref ((PolKitAction *) l->data);
                }
                g_slist_free (action->priv->polkit_action_sufficient);
                action->priv->polkit_action_sufficient = NULL;
        }
}

static void
polkit_gnome_action_finalize (GObject *object)
{
        PolKitGnomeAction *action;

        action = POLKIT_GNOME_ACTION (object);

        if (action->priv->polkit_action != NULL)
                polkit_action_unref (action->priv->polkit_action);

        g_free (action->priv->self_blocked_short_label);
        g_free (action->priv->self_blocked_label);
        g_free (action->priv->self_blocked_tooltip);
        g_free (action->priv->self_blocked_icon_name);

        g_free (action->priv->no_short_label);
        g_free (action->priv->no_label);
        g_free (action->priv->no_tooltip);
        g_free (action->priv->no_icon_name);

        g_free (action->priv->auth_short_label);
        g_free (action->priv->auth_label);
        g_free (action->priv->auth_tooltip);
        g_free (action->priv->auth_icon_name);

        g_free (action->priv->yes_short_label);
        g_free (action->priv->yes_label);
        g_free (action->priv->yes_tooltip);
        g_free (action->priv->yes_icon_name);

        free_pk_action_sufficient (action);

        if (action->priv->pk_g_context != NULL) {
                g_signal_handler_disconnect (action->priv->pk_g_context, action->priv->config_changed_handler_id);
                g_signal_handler_disconnect (action->priv->pk_g_context, action->priv->console_kit_db_changed_handler_id);
                g_object_unref (action->priv->pk_g_context);
        }

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

static void
_ensure_pk_g_context (PolKitGnomeAction *action)
{
        if (action->priv->pk_g_context == NULL) {
                action->priv->pk_g_context = polkit_gnome_context_get (NULL);
                
                action->priv->config_changed_handler_id = g_signal_connect (
                        action->priv->pk_g_context,
                        "config-changed",
                        G_CALLBACK (_pk_config_changed),
                        action);
                
                action->priv->console_kit_db_changed_handler_id = g_signal_connect (
                        action->priv->pk_g_context,
                        "console-kit-db-changed",
                        G_CALLBACK (_pk_console_kit_db_changed),
                        action);
        }
}

static GObject *
polkit_gnome_action_constructor (GType                  type,
                                 guint                  n_construct_properties,
                                 GObjectConstructParam *construct_properties)
{
        PolKitGnomeAction      *action;
        PolKitGnomeActionClass *klass;

        klass = POLKIT_GNOME_ACTION_CLASS (g_type_class_peek (POLKIT_GNOME_TYPE_ACTION));

        action = POLKIT_GNOME_ACTION (G_OBJECT_CLASS (parent_class)->constructor (type,
                                                                                   n_construct_properties,
                                                                                   construct_properties));

        action->priv->master_visible = TRUE;
        action->priv->master_sensitive = TRUE;

        return G_OBJECT (action);
}


static void
polkit_gnome_action_class_init (PolKitGnomeActionClass *klass)
{
        GObjectClass *gobject_class;
        GtkActionClass *action_class;

        parent_class = g_type_class_peek_parent (klass);
        gobject_class = G_OBJECT_CLASS (klass);
        action_class = GTK_ACTION_CLASS (klass);

        gobject_class->constructor = polkit_gnome_action_constructor;
        gobject_class->set_property = set_property;
        gobject_class->get_property = get_property;
        gobject_class->finalize = polkit_gnome_action_finalize;

        action_class->activate = (void (*)(GtkAction*)) polkit_gnome_action_activate;

        klass->auth_start = _auth_start;

        g_object_class_install_property (gobject_class,
                                         PROP_POLKIT_ACTION_OBJ,
                                         g_param_spec_pointer ("polkit-action",
                                                               "The PolKitAction object this GTK+ action is tracking",
                                                               "The PolKitAction object this GTK+ action is tracking",
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

        g_object_class_install_property (gobject_class,
                                         PROP_POLKIT_ACTION_OBJ_SUFFICIENT,
                                         g_param_spec_value_array (
                                                 "polkit-action-sufficient",
                                                 "An array of PolKitAction objects that are sufficient to have authorizations for.",
                                                 "An array of PolKitAction objects that are sufficient to have authorizations for.",
                                                 g_param_spec_pointer (
                                                         "polkit-action-sufficient-member",
                                                         "PolKitAction member of polkit-action-sufficient",
                                                         "PolKitAction member of polkit-action-sufficient",
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT),
                                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

        /*------------------------------*/

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_SELF_BLOCKED_VISIBLE,
                g_param_spec_boolean (
                        "self-blocked-visible",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, whether the action will be visible",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, whether the action will be visible",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_SELF_BLOCKED_SENSITIVE,
                g_param_spec_boolean (
                        "self-blocked-sensitive",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, whether the action will be sensitive",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, whether the action will be sensitive",
                        FALSE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_SELF_BLOCKED_SHORT_LABEL,
                g_param_spec_string (
                        "self-blocked-short-label",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this short-label",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this short-label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_SELF_BLOCKED_LABEL,
                g_param_spec_string (
                        "self-blocked-label",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this label",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_SELF_BLOCKED_TOOLTIP,
                g_param_spec_string (
                        "self-blocked-tooltip",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this tooltip",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this tooltip",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_SELF_BLOCKED_ICON_NAME,
                g_param_spec_string (
                        "self-blocked-icon-name",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this icon-name",
                        "If PolicyKit evaluates the result as 'no' and the reason is that the user has a self-granted negative authorization, use this icon-name",
                        NULL,
                        G_PARAM_READWRITE));

        /*------------------------------*/

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_NO_VISIBLE,
                g_param_spec_boolean (
                        "no-visible",
                        "If PolicyKit evaluates the result as 'no', whether the action will be visible",
                        "If PolicyKit evaluates the result as 'no', whether the action will be visible",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_NO_SENSITIVE,
                g_param_spec_boolean (
                        "no-sensitive",
                        "If PolicyKit evaluates the result as 'no', whether the action will be sensitive",
                        "If PolicyKit evaluates the result as 'no', whether the action will be sensitive",
                        FALSE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_NO_SHORT_LABEL,
                g_param_spec_string (
                        "no-short-label",
                        "If PolicyKit evaluates the result as 'no', use this short-label",
                        "If PolicyKit evaluates the result as 'no', use this short-label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_NO_LABEL,
                g_param_spec_string (
                        "no-label",
                        "If PolicyKit evaluates the result as 'no', use this label",
                        "If PolicyKit evaluates the result as 'no', use this label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_NO_TOOLTIP,
                g_param_spec_string (
                        "no-tooltip",
                        "If PolicyKit evaluates the result as 'no', use this tooltip",
                        "If PolicyKit evaluates the result as 'no', use this tooltip",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_NO_ICON_NAME,
                g_param_spec_string (
                        "no-icon-name",
                        "If PolicyKit evaluates the result as 'no', use this icon-name",
                        "If PolicyKit evaluates the result as 'no', use this icon-name",
                        NULL,
                        G_PARAM_READWRITE));

        /*------------------------------*/

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_AUTH_VISIBLE,
                g_param_spec_boolean (
                        "auth-visible",
                        "If PolicyKit evaluates the result as 'auth', whether the action will be visible",
                        "If PolicyKit evaluates the result as 'auth', whether the action will be visible",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_AUTH_SENSITIVE,
                g_param_spec_boolean (
                        "auth-sensitive",
                        "If PolicyKit evaluates the result as 'auth', whether the action will be sensitive",
                        "If PolicyKit evaluates the result as 'auth', whether the action will be sensitive",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_AUTH_SHORT_LABEL,
                g_param_spec_string (
                        "auth-short-label",
                        "If PolicyKit evaluates the result as 'auth', use this short-label",
                        "If PolicyKit evaluates the result as 'auth', use this short-label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_AUTH_LABEL,
                g_param_spec_string (
                        "auth-label",
                        "If PolicyKit evaluates the result as 'auth', use this label",
                        "If PolicyKit evaluates the result as 'auth', use this label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_AUTH_TOOLTIP,
                g_param_spec_string (
                        "auth-tooltip",
                        "If PolicyKit evaluates the result as 'auth', use this tooltip",
                        "If PolicyKit evaluates the result as 'auth', use this tooltip",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_AUTH_ICON_NAME,
                g_param_spec_string (
                        "auth-icon-name",
                        "If PolicyKit evaluates the result as 'auth', use this icon-name",
                        "If PolicyKit evaluates the result as 'auth', use this icon-name",
                        NULL,
                        G_PARAM_READWRITE));

        /*------------------------------*/

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_YES_VISIBLE,
                g_param_spec_boolean (
                        "yes-visible",
                        "If PolicyKit evaluates the result as 'yes', whether the action will be visible",
                        "If PolicyKit evaluates the result as 'yes', whether the action will be visible",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_YES_SENSITIVE,
                g_param_spec_boolean (
                        "yes-sensitive",
                        "If PolicyKit evaluates the result as 'yes', whether the action will be sensitive",
                        "If PolicyKit evaluates the result as 'yes', whether the action will be sensitive",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_YES_SHORT_LABEL,
                g_param_spec_string (
                        "yes-short-label",
                        "If PolicyKit evaluates the result as 'yes', use this short-label",
                        "If PolicyKit evaluates the result as 'yes', use this short-label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_YES_LABEL,
                g_param_spec_string (
                        "yes-label",
                        "If PolicyKit evaluates the result as 'yes', use this label",
                        "If PolicyKit evaluates the result as 'yes', use this label",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_YES_TOOLTIP,
                g_param_spec_string (
                        "yes-tooltip",
                        "If PolicyKit evaluates the result as 'yes', use this tooltip",
                        "If PolicyKit evaluates the result as 'yes', use this tooltip",
                        NULL,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_YES_ICON_NAME,
                g_param_spec_string (
                        "yes-icon-name",
                        "If PolicyKit evaluates the result as 'yes', use this icon-name",
                        "If PolicyKit evaluates the result as 'yes', use this icon-name",
                        NULL,
                        G_PARAM_READWRITE));

        /*------------------------------*/

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_MASTER_VISIBLE,
                g_param_spec_boolean (
                        "master-visible",
                        "Can be set to FALSE to force invisibility no matter what PolicyKit reports",
                        "Can be set to FALSE to force invisibility no matter what PolicyKit reports",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_MASTER_SENSITIVE,
                g_param_spec_boolean (
                        "master-sensitive",
                        "Can be set to FALSE to force insensitivity no matter what PolicyKit reports",
                        "Can be set to FALSE to force insensitivity no matter what PolicyKit reports",
                        TRUE,
                        G_PARAM_READWRITE));

        g_object_class_install_property (
                gobject_class,
                PROP_POLKIT_TARGET_PID,
                g_param_spec_uint (
                        "target-pid",
                        "The target process id to receive the authorization; if 0 it is the current process",
                        "The target process id to receive the authorization; if 0 it is the current process",
                        0,
                        G_MAXUINT,
                        0,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

        /**
         * PolKitGnomeAction::auth-start:
         * @action: the object
         *
         * The ::auth-start signal is emitted when an authentication
         * session starts.
         **/
        signals [AUTH_START_SIGNAL] =
                g_signal_new ("auth-start",
                              G_TYPE_FROM_CLASS (gobject_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PolKitGnomeActionClass, auth_start),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);

        /**
         * PolKitGnomeAction::auth-end:
         * @action: the object
         * @gained_privilege: whether the privilege was gained
         *
         * The ::auth-end signal is emitted when the an authentication
         * session ends and carries information about whether the
         * privilege was obtained or not.
         **/
        signals [AUTH_END_SIGNAL] =
                g_signal_new ("auth-end",
                              G_TYPE_FROM_CLASS (gobject_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PolKitGnomeActionClass, auth_end),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__BOOLEAN,
                              G_TYPE_NONE,
                              1, G_TYPE_BOOLEAN);

        /**
         * PolKitGnomeAction::polkit-result-changed:
         * @action: the object
         * @current_result: current #PolKitResult from PolicyKit regarding given #PolKitAction object
         *
         * The ::polkit-result-changed signal is emitted when the
         * PolicyKit result changes. This can happen when external
         * factors (config file, ConsoleKit, privilege granted /
         * revoked) change since the #PolKitGnomeAction class listens
         * for events using the #PolKitGnomeContext class.
         **/
        signals [POLKIT_RESULT_CHANGED_SIGNAL] =
                g_signal_new ("polkit-result-changed",
                              G_TYPE_FROM_CLASS (gobject_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PolKitGnomeActionClass, polkit_result_changed),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__INT,
                              G_TYPE_NONE,
                              1, G_TYPE_INT);


        g_type_class_add_private (gobject_class, sizeof (PolKitGnomeActionPrivate));
}

static pid_t
_get_target_pid (PolKitGnomeAction *action)
{
        pid_t pid;

        if (action != NULL && action->priv->target_pid != 0)
                pid = action->priv->target_pid;
        else
                pid = getpid ();

        return pid;
}

static PolKitResult
_compute_polkit_result_direct  (PolKitGnomeAction *action)
{
        PolKitCaller *pk_caller;
        PolKitResult pk_result;
        DBusError dbus_error;

        _ensure_pk_g_context (action);

        dbus_error_init (&dbus_error);        
        pk_caller = polkit_tracker_get_caller_from_pid (action->priv->pk_g_context->pk_tracker, 
                                                        _get_target_pid (action),
                                                        &dbus_error);
        if (pk_caller == NULL) {
                g_warning ("Cannot get PolKitCaller object for target (pid=%d): %s: %s",
                           _get_target_pid (action), dbus_error.name, dbus_error.message);
                dbus_error_free (&dbus_error);

                /* this is bad so cop-out to UKNOWN */
                pk_result = POLKIT_RESULT_UNKNOWN;
        } else {
                pk_result = polkit_context_is_caller_authorized (action->priv->pk_g_context->pk_context, 
                                                                 action->priv->polkit_action, 
                                                                 pk_caller,
                                                                 FALSE,
                                                                 NULL);
                if (pk_result != POLKIT_RESULT_YES) {
                        GSList *i;

                        /* no dice.. see if one if the sufficient actions, if any, yields a YES */
                        for (i = action->priv->polkit_action_sufficient; i != NULL; i = g_slist_next (i)) {
                                PolKitResult r;
                                PolKitAction *a = (PolKitAction *) i->data;

                                r = polkit_context_is_caller_authorized (action->priv->pk_g_context->pk_context, 
                                                                         a, 
                                                                         pk_caller,
                                                                         FALSE,
                                                                         NULL);

                                if (r == POLKIT_RESULT_YES) {
                                        pk_result = r;
                                        break;
                                }
                        }
                }
        }

        if (pk_caller != NULL)
                polkit_caller_unref (pk_caller);

        return pk_result;
}

/* returns TRUE if the result changed */
static gboolean
_compute_polkit_result (PolKitGnomeAction *action)
{
        PolKitResult old_result;

        old_result = action->priv->pk_result;
        action->priv->pk_result = POLKIT_RESULT_UNKNOWN;

        if (action->priv->polkit_action == NULL) {
                action->priv->pk_result = POLKIT_RESULT_YES;
        } else {
                action->priv->pk_result = _compute_polkit_result_direct (action);
        }

        return old_result != action->priv->pk_result;
}

static void
_update_action (PolKitGnomeAction *action)
{
        PolKitGnomeContext *pkgc;
        PolKitAuthorizationDB *authdb;
                                
        pkgc = polkit_gnome_context_get (NULL);
        authdb = polkit_context_get_authorization_db (pkgc->pk_context);

        switch (action->priv->pk_result) {
        default:
        case POLKIT_RESULT_UNKNOWN:
        case POLKIT_RESULT_NO:
                /* TODO: see if we're self-blocked */

                if (action->priv->polkit_action != NULL && 
                    polkit_authorization_db_is_uid_blocked_by_self (authdb, 
                                                                    action->priv->polkit_action,
                                                                    getuid (),
                                                                    NULL)) {
                        g_object_set (action, 
                                      "visible", action->priv->self_blocked_visible && action->priv->master_visible,
                                      "sensitive", action->priv->self_blocked_sensitive && action->priv->master_sensitive,
                                      "short-label", action->priv->self_blocked_short_label,
                                      "label", action->priv->self_blocked_label,
                                      "tooltip", action->priv->self_blocked_tooltip,
                                      "icon-name", action->priv->self_blocked_icon_name, 
                                      NULL);
                } else {
                        g_object_set (action, 
                                      "visible", action->priv->no_visible && action->priv->master_visible,
                                      "sensitive", action->priv->no_sensitive && action->priv->master_sensitive,
                                      "short-label", action->priv->no_short_label,
                                      "label", action->priv->no_label,
                                      "tooltip", action->priv->no_tooltip,
                                      "icon-name", action->priv->no_icon_name, 
                                      NULL);
                }
                break;

        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_ONE_SHOT:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_SESSION:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_ALWAYS:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_ONE_SHOT:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_SESSION:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_ALWAYS:
                g_object_set (action, 
                              "visible", action->priv->auth_visible && action->priv->master_visible,
                              "sensitive", action->priv->auth_sensitive && action->priv->master_sensitive,
                              "short-label", action->priv->auth_short_label,
                              "label", action->priv->auth_label,
                              "tooltip", action->priv->auth_tooltip,
                              "icon-name", action->priv->auth_icon_name, 
                              NULL);
                break;

        case POLKIT_RESULT_YES:
                g_object_set (action, 
                              "visible", action->priv->yes_visible && action->priv->master_visible,
                              "sensitive", action->priv->yes_sensitive && action->priv->master_sensitive,
                              "short-label", action->priv->yes_short_label,
                              "label", action->priv->yes_label,
                              "tooltip", action->priv->yes_tooltip,
                              "icon-name", action->priv->yes_icon_name,
                              NULL);
                break;
        }
}

static void
_pk_config_changed (PolKitGnomeContext *pk_g_context, PolKitGnomeAction  *action)
{
        gboolean result_changed;
        result_changed = _compute_polkit_result (action);
        _update_action (action);
        if (result_changed) {
                g_signal_emit (action, signals [POLKIT_RESULT_CHANGED_SIGNAL], 0, action->priv->pk_result);
        }
}

static void
_pk_console_kit_db_changed (PolKitGnomeContext *pk_g_context, PolKitGnomeAction  *action)
{
        /* for now, just use the same code as above... */
        _pk_config_changed (pk_g_context, action);
}


/**
 * polkit_gnome_action_new:
 * @name: A unique name for the action
 *
 * Creates a new #PolKitGnomeAction object. The typical use for this
 * function is for specialized use where
 * polkit_gnome_action_new_default() does not meet the needs of the
 * application. A short example of the usage of this contructor
 * follows.
 *
 * If the 'polkit-action' property is #NULL the behavior is similar to
 * as if a #PolKitAction returned #POLKIT_RESULT_YES.
 *
 * One can set the 'polkit-action-sufficient' property to a
 * #GValueArray of pointers to #PolKitAction objects. The semantics of
 * this property is that if the user is authorized for any of the
 * given actions in 'polkit-action-sufficient' then the behavior is
 * the same as if they're authorized for the action denoted by
 * 'polkit-action'. This is useful in a setup where you have two
 * similar actions and one implies the other. For example, in
 * gnome-system-monitor there are two actions
 * <literal>org.gnome.system-monitor.increase-own-priority</literal>
 * (Increase the priority of a process owned by yourself) and
 * <literal>org.gnome.system-monitor.change-priorty</literal> (Change
 * priority of any process). As the latter clearly implies the former,
 * one would set the latter in the 'polkit-action-sufficient'
 * property when constructing a #PolKitAction for the former.
 *
 * <programlisting>
 * PolKitAction *polkit_action;
 * PolKitGnomeAction *action;
 *
 * polkit_action = polkit_action_new ();
 * polkit_action_set_action_id (polkit_action, "org.example.some-policykit-action");
 *
 * action = polkit_gnome_action_new ("blabla", NULL);
 * g_object_set (action,
 *               "polkit-action",    polkit_action,
 *               "no-visible",       TRUE,
 *               "no-sensitive",     FALSE,
 *               "no-short-label",   "The Action no can do!",
 *               "no-label",         "The Action is not permitted!",
 *               "no-tooltip",       "The Tooltip (no)",
 *               "no-icon-name",     GTK_STOCK_NO,
 *
 *               "auth-visible",     TRUE,
 *               "auth-sensitive",   TRUE,
 *               "auth-short-label", "The Action requires auth...",
 *               "auth-label",       "The Action requires auth...",
 *               "auth-tooltip",     "The Tooltip (auth)",
 *               "auth-icon-name",   GTK_STOCK_DIALOG_AUTHENTICATION,
 *
 *               "yes-visible",      TRUE,
 *               "yes-sensitive",    TRUE,
 *               "yes-short-label",  "Action!",
 *               "yes-label",        "Just do the Action!",
 *               "yes-tooltip",      "The Tooltip (yes)",
 *               "yes-icon-name",    GTK_STOCK_YES,
 *               NULL);
 * </programlisting>
 *
 * Returns: a new #PolKitGnomeAction or #NULL if error is set
 */
PolKitGnomeAction *
polkit_gnome_action_new (const gchar *name)
{
        PolKitGnomeAction *action = NULL;

        action = g_object_new (POLKIT_GNOME_TYPE_ACTION,
                               "name", name,
                               NULL);

        return action;
}

/**
 * polkit_gnome_action_new_default:
 * @name: A unique name for the action
 * @polkit_action: the #PolKitAction to track
 * @label: the label to use (will also apply to short-label)
 * @tooltip: the tool tip to use
 *
 * Creates a new #PolKitGnomeAction object with the default
 * behavior for a given #PolKitAction object.
 *
 * Default behavior is defined by the label and tooltip being
 * identical across all three four states, the action being visible in
 * all four states, and the action being insensitive only in the state
 * where the result from PolicyKit is no and self-blocked. Only when
 * PolicyKit returns one of the 'auth*' results, the icon_name
 * property will be set to #GTK_STOCK_DIALOG_AUTHENTICATION.
 *
 * The caller can always modify individual aspects of the action after
 * creation, e.g. change the tooltip for the self-blocked, no, auth
 * and yes states.
 *
 * If the given polkit_action is #NULL the behavior is similar to as
 * if a #PolKitAction returned #POLKIT_RESULT_YES.
 *
 * Returns: a new #PolKitGnomeAction or #NULL if error is set
 */
PolKitGnomeAction *
polkit_gnome_action_new_default (const gchar  *name, 
                                 PolKitAction *polkit_action, 
                                 const gchar  *label, 
                                 const gchar  *tooltip)
{
        PolKitGnomeAction *action;

        action = g_object_new (POLKIT_GNOME_TYPE_ACTION,
                               "name", name,
                               "polkit-action", polkit_action,

                               "self-blocked-visible",       TRUE,
                               "self-blocked-sensitive",     FALSE,
                               "self-blocked-short-label",   label,
                               "self-blocked-label",         label,
                               "self-blocked-tooltip",       tooltip,
                               "self-blocked-icon-name",     NULL,

                               "no-visible",       TRUE,
                               "no-sensitive",     FALSE,
                               "no-short-label",   label,
                               "no-label",         label,
                               "no-tooltip",       tooltip,
                               "no-icon-name",     NULL,
                               
                               "auth-visible",     TRUE,
                               "auth-sensitive",   TRUE,
                               "auth-short-label", label,
                               "auth-label",       label,
                               "auth-tooltip",     tooltip,
                               "auth-icon-name",   GTK_STOCK_DIALOG_AUTHENTICATION,
                               
                               "yes-visible",      TRUE,
                               "yes-sensitive",    TRUE,
                               "yes-short-label",  label,
                               "yes-label",        label,
                               "yes-tooltip",      tooltip,
                               "yes-icon-name",    NULL,
                               
                               "master-visible",   TRUE,
                               "master-sensitive", TRUE,
                               NULL);

        return action;
}

/*----------------------------------------------------------------------------------------------------*/

/**
 * polkit_gnome_action_set_polkit_action:
 * @action: The #PolKitGnomeAction object
 * @pk_action: The #PolKitAction object
 *
 * Sets the #PolKitAction object to track for updating the GTK+ action.
 */
static void
polkit_gnome_action_set_polkit_action (PolKitGnomeAction *action, PolKitAction *pk_action)
{

        /* Don't bother updating polkit_action if it's the same
         * value.. it will just cause a lot of unnecessary work as
         * we'll recompute the answer via PolicyKit.. 
         *
         * unless it's on the initial call (where priv->polkit_action
         * is alread NULL) because we need that initial update;
         */
        if (!action->priv->polkit_action_set_once || action->priv->polkit_action != pk_action) {

                action->priv->polkit_action_set_once = TRUE;

                if (action->priv->polkit_action != NULL)
                        polkit_action_unref (action->priv->polkit_action);

                action->priv->polkit_action = pk_action != NULL ? polkit_action_ref (pk_action) : NULL;
                
                _compute_polkit_result (action);
                _update_action (action);
        }
}

static void 
polkit_gnome_action_set_polkit_action_sufficient (PolKitGnomeAction *action, const GValue *pk_action_array)
{
        unsigned int n;
        GValueArray *value_array;

        free_pk_action_sufficient (action);

        if (pk_action_array == NULL)
                goto out;

	value_array = g_value_get_boxed (pk_action_array);
        if (value_array == NULL)
                goto out;

	for (n = 0; n < value_array->n_values; n++) {
                PolKitAction *pk_action;

                pk_action = (PolKitAction *) g_value_get_pointer (& (value_array->values[n]));
		action->priv->polkit_action_sufficient = g_slist_prepend (action->priv->polkit_action_sufficient, 
                                                                          polkit_action_ref (pk_action));
                char *s;
                polkit_action_get_action_id (pk_action, &s);
                g_warning ("Setting sufficient %d: %s", n, s);
	}

out:
        _compute_polkit_result (action);
        _update_action (action);
}


/**
 * polkit_gnome_action_get_polkit_result:
 * @action: The #PolKitGnomeAction object
 *
 * Gets the #PolKitResult that indicates whether the user is
 * privileged to do the #PolKitAction associated with this
 * #PolKitGnomeAction object.
 *
 * Returns: The #PolKitAction object. The caller shall not unref this object.
 */
PolKitResult
polkit_gnome_action_get_polkit_result (PolKitGnomeAction *action)
{
        _compute_polkit_result (action);
        _update_action (action);
        return action->priv->pk_result;
}

/**
 * polkit_gnome_action_get_sensitive:
 * @action: The #PolKitGnomeAction object
 *
 * Get the master sensitivity, see PolKitGnomeAction:master-sensitive:
 * for details.
 * 
 * Returns: the master sensitivity
 */
gboolean
polkit_gnome_action_get_sensitive (PolKitGnomeAction *action)
{
        return action->priv->master_sensitive;
}

/**
 * polkit_gnome_action_set_sensitive:
 * @action: The #PolKitGnomeAction object
 * @sensitive: master sensitivity
 *
 * Set the master sensitivity, see PolKitGnomeAction:master-sensitive:
 * for details.
 */
void
polkit_gnome_action_set_sensitive (PolKitGnomeAction *action, gboolean sensitive)
{
        if (action->priv->master_sensitive == sensitive)
                return;
        action->priv->master_sensitive = sensitive;
        _update_action (action);
}

static void 
polkit_gnome_action_set_target_pid (PolKitGnomeAction *action, guint target_pid)
{
        action->priv->target_pid = target_pid;
        _compute_polkit_result (action);
        _update_action (action);
}

/**
 * polkit_gnome_action_get_visible:
 * @action: The #PolKitGnomeAction object
 *
 * Get the master visibility, see PolKitGnomeAction:master-visible:
 * for details.
 * 
 * Returns: the master visibility
 */
gboolean
polkit_gnome_action_get_visible (PolKitGnomeAction *action)
{
        return action->priv->master_visible;
}

/**
 * polkit_gnome_action_set_visible:
 * @action: The #PolKitGnomeAction object
 * @visible: master visibility
 *
 * Set the master visibility, see PolKitGnomeAction:master-visible:
 * for details.
 */
void
polkit_gnome_action_set_visible (PolKitGnomeAction *action, gboolean visible)
{
        if (action->priv->master_visible == visible)
                return;
        action->priv->master_visible = visible;
        _update_action (action);
}

/*----------------------------------------------------------------------------------------------------*/

/**
 * polkit_gnome_action_set_self_blocked_visible:
 * @action: The #PolKitGnomeAction object
 * @visible: new value
 *
 * Sets the value of visible to use when PolicyKit returns the answer
 * no (and the reason is that the user has a self-granted negative
 * authorization) for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_self_blocked_visible (PolKitGnomeAction *action, gboolean visible)
{
        action->priv->self_blocked_visible = visible;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_self_blocked_sensitive:
 * @action: The #PolKitGnomeAction object
 * @sensitive: new value
 *
 * Sets the value of sensitive to use when PolicyKit returns the
 * answer no (and the reason is that the user has a self-granted
 * negative authorization) for the current #PolKitAction being
 * tracked.
 */
static void
polkit_gnome_action_set_self_blocked_sensitive (PolKitGnomeAction *action, gboolean sensitive)
{
        action->priv->self_blocked_sensitive = sensitive;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_self_blocked_short_label:
 * @action: The #PolKitGnomeAction object
 * @short_label: new value
 *
 * Sets the value of short-label to use when PolicyKit returns the
 * answer no (and the reason is that the user has a self-granted
 * negative authorization) for the current #PolKitAction being
 * tracked.
 */
static void
polkit_gnome_action_set_self_blocked_short_label (PolKitGnomeAction *action, const gchar *short_label)
{
        g_free (action->priv->self_blocked_short_label);
        action->priv->self_blocked_short_label = g_strdup (short_label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_self_blocked_label:
 * @action: The #PolKitGnomeAction object
 * @label: new value
 *
 * Sets the value of label to use when PolicyKit returns the answer
 * no (and the reason is that the user has a self-granted
 * negative authorization) for the current #PolKitAction being
 * tracked.
 */
static void
polkit_gnome_action_set_self_blocked_label (PolKitGnomeAction *action, const gchar *label)
{
        g_free (action->priv->self_blocked_label);
        action->priv->self_blocked_label = g_strdup (label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_self_blocked_tooltip:
 * @action: The #PolKitGnomeAction object
 * @tooltip: new value
 *
 * Sets the value of tooltip to use when PolicyKit returns the answer
 * no (and the reason is that the user has a self-granted negative
 * authorization) for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_self_blocked_tooltip (PolKitGnomeAction *action, const gchar *tooltip)
{
        g_free (action->priv->self_blocked_tooltip);
        action->priv->self_blocked_tooltip = g_strdup (tooltip);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_self_blocked_icon_name:
 * @action: The #PolKitGnomeAction object
 * @icon_name: new value
 *
 * Sets the value of icon_name to use when PolicyKit returns the
 * answer no (and the reason is that the user has a self-granted
 * negative authorization) for the current #PolKitAction being
 * tracked.
 */
static void
polkit_gnome_action_set_self_blocked_icon_name (PolKitGnomeAction *action, const gchar *icon_name)
{
        g_free (action->priv->self_blocked_icon_name);
        action->priv->self_blocked_icon_name = g_strdup (icon_name);
        _update_action (action);
}

/*----------------------------------------------------------------------------------------------------*/

/**
 * polkit_gnome_action_set_no_visible:
 * @action: The #PolKitGnomeAction object
 * @visible: new value
 *
 * Sets the value of visible to use when PolicyKit returns the answer
 * no for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_no_visible (PolKitGnomeAction *action, gboolean visible)
{
        action->priv->no_visible = visible;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_no_sensitive:
 * @action: The #PolKitGnomeAction object
 * @sensitive: new value
 *
 * Sets the value of sensitive to use when PolicyKit returns the answer
 * no for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_no_sensitive (PolKitGnomeAction *action, gboolean sensitive)
{
        action->priv->no_sensitive = sensitive;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_no_short_label:
 * @action: The #PolKitGnomeAction object
 * @short_label: new value
 *
 * Sets the value of short-label to use when PolicyKit returns the
 * answer no for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_no_short_label (PolKitGnomeAction *action, const gchar *short_label)
{
        g_free (action->priv->no_short_label);
        action->priv->no_short_label = g_strdup (short_label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_no_label:
 * @action: The #PolKitGnomeAction object
 * @label: new value
 *
 * Sets the value of label to use when PolicyKit returns the answer no
 * for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_no_label (PolKitGnomeAction *action, const gchar *label)
{
        g_free (action->priv->no_label);
        action->priv->no_label = g_strdup (label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_no_tooltip:
 * @action: The #PolKitGnomeAction object
 * @tooltip: new value
 *
 * Sets the value of tooltip to use when PolicyKit returns the answer
 * no for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_no_tooltip (PolKitGnomeAction *action, const gchar *tooltip)
{
        g_free (action->priv->no_tooltip);
        action->priv->no_tooltip = g_strdup (tooltip);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_no_icon_name:
 * @action: The #PolKitGnomeAction object
 * @icon_name: new value
 *
 * Sets the value of icon_name to use when PolicyKit returns the
 * answer no for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_no_icon_name (PolKitGnomeAction *action, const gchar *icon_name)
{
        g_free (action->priv->no_icon_name);
        action->priv->no_icon_name = g_strdup (icon_name);
        _update_action (action);
}

/*----------------------------------------------------------------------------------------------------*/

/**
 * polkit_gnome_action_set_auth_visible:
 * @action: The #PolKitGnomeAction object
 * @visible: new value
 *
 * Sets the value of visible to use when PolicyKit returns the answer
 * auth* for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_auth_visible (PolKitGnomeAction *action, gboolean visible)
{
        action->priv->auth_visible = visible;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_auth_sensitive:
 * @action: The #PolKitGnomeAction object
 * @sensitive: new value
 *
 * Sets the value of sensitive to use when PolicyKit returns the answer
 * auth* for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_auth_sensitive (PolKitGnomeAction *action, gboolean sensitive)
{
        action->priv->auth_sensitive = sensitive;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_auth_short_label:
 * @action: The #PolKitGnomeAction object
 * @short_label: new value
 *
 * Sets the value of short-label to use when PolicyKit returns the
 * answer auth for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_auth_short_label (PolKitGnomeAction *action, const gchar *short_label)
{
        g_free (action->priv->auth_short_label);
        action->priv->auth_short_label = g_strdup (short_label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_auth_label:
 * @action: The #PolKitGnomeAction object
 * @label: new value
 *
 * Sets the value of label to use when PolicyKit returns the answer auth*
 * for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_auth_label (PolKitGnomeAction *action, const gchar *label)
{
        g_free (action->priv->auth_label);
        action->priv->auth_label = g_strdup (label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_auth_tooltip:
 * @action: The #PolKitGnomeAction object
 * @tooltip: new value
 *
 * Sets the value of tooltip to use when PolicyKit returns the answer
 * auth* for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_auth_tooltip (PolKitGnomeAction *action, const gchar *tooltip)
{
        g_free (action->priv->auth_tooltip);
        action->priv->auth_tooltip = g_strdup (tooltip);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_auth_icon_name:
 * @action: The #PolKitGnomeAction object
 * @icon_name: new value
 *
 * Sets the value of icon_name to use when PolicyKit returns the
 * answer auth* for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_auth_icon_name (PolKitGnomeAction *action, const gchar *icon_name)
{
        g_free (action->priv->auth_icon_name);
        action->priv->auth_icon_name = g_strdup (icon_name);
        _update_action (action);
}

/*----------------------------------------------------------------------------------------------------*/

/**
 * polkit_gnome_action_set_yes_visible:
 * @action: The #PolKitGnomeAction object
 * @visible: new value
 *
 * Sets the value of visible to use when PolicyKit returns the answer
 * yes for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_yes_visible (PolKitGnomeAction *action, gboolean visible)
{
        action->priv->yes_visible = visible;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_yes_sensitive:
 * @action: The #PolKitGnomeAction object
 * @sensitive: new value
 *
 * Sets the value of sensitive to use when PolicyKit returns the answer
 * yes for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_yes_sensitive (PolKitGnomeAction *action, gboolean sensitive)
{
        action->priv->yes_sensitive = sensitive;
        _update_action (action);
}

/**
 * polkit_gnome_action_set_yes_short_label:
 * @action: The #PolKitGnomeAction object
 * @short_label: new value
 *
 * Sets the value of short-label to use when PolicyKit returns the
 * answer yes for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_yes_short_label (PolKitGnomeAction *action, const gchar *short_label)
{
        g_free (action->priv->yes_short_label);
        action->priv->yes_short_label = g_strdup (short_label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_yes_label:
 * @action: The #PolKitGnomeAction object
 * @label: new value
 *
 * Sets the value of label to use when PolicyKit returns the answer yes
 * for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_yes_label (PolKitGnomeAction *action, const gchar *label)
{
        g_free (action->priv->yes_label);
        action->priv->yes_label = g_strdup (label);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_yes_tooltip:
 * @action: The #PolKitGnomeAction object
 * @tooltip: new value
 *
 * Sets the value of tooltip to use when PolicyKit returns the answer
 * yes for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_yes_tooltip (PolKitGnomeAction *action, const gchar *tooltip)
{
        g_free (action->priv->yes_tooltip);
        action->priv->yes_tooltip = g_strdup (tooltip);
        _update_action (action);
}

/**
 * polkit_gnome_action_set_yes_icon_name:
 * @action: The #PolKitGnomeAction object
 * @icon_name: new value
 *
 * Sets the value of icon_name to use when PolicyKit returns the
 * answer yes for the current #PolKitAction being tracked.
 */
static void
polkit_gnome_action_set_yes_icon_name (PolKitGnomeAction *action, const gchar *icon_name)
{
        g_free (action->priv->yes_icon_name);
        action->priv->yes_icon_name = g_strdup (icon_name);
        _update_action (action);
}

/*----------------------------------------------------------------------------------------------------*/

static void
get_property (GObject     *object,
	      guint        prop_id,
	      GValue      *value,
	      GParamSpec  *pspec)
{
        PolKitGnomeAction *action = POLKIT_GNOME_ACTION (object);
        
        switch (prop_id)
        {
        case PROP_POLKIT_ACTION_OBJ:
                g_value_set_pointer (value, action->priv->polkit_action != NULL ? polkit_action_ref (action->priv->polkit_action) : NULL);
                break;

        case PROP_POLKIT_ACTION_OBJ_SUFFICIENT:
                //TODO: g_value_set_pointer (value, action->priv->polkit_action != NULL ? polkit_action_ref (action->priv->polkit_action) : NULL);
                break;

        case PROP_POLKIT_SELF_BLOCKED_VISIBLE:
                g_value_set_boolean (value, action->priv->self_blocked_visible);
                break;
        case PROP_POLKIT_SELF_BLOCKED_SENSITIVE:
                g_value_set_boolean (value, action->priv->self_blocked_sensitive);
                break;
        case PROP_POLKIT_SELF_BLOCKED_SHORT_LABEL:
                g_value_set_string (value, action->priv->self_blocked_short_label);
                break;
        case PROP_POLKIT_SELF_BLOCKED_LABEL:
                g_value_set_string (value, action->priv->self_blocked_label);
                break;
        case PROP_POLKIT_SELF_BLOCKED_TOOLTIP:
                g_value_set_string (value, action->priv->self_blocked_tooltip);
                break;
        case PROP_POLKIT_SELF_BLOCKED_ICON_NAME:
                g_value_set_string (value, action->priv->self_blocked_icon_name);
                break;

        case PROP_POLKIT_NO_VISIBLE:
                g_value_set_boolean (value, action->priv->no_visible);
                break;
        case PROP_POLKIT_NO_SENSITIVE:
                g_value_set_boolean (value, action->priv->no_sensitive);
                break;
        case PROP_POLKIT_NO_SHORT_LABEL:
                g_value_set_string (value, action->priv->no_short_label);
                break;
        case PROP_POLKIT_NO_LABEL:
                g_value_set_string (value, action->priv->no_label);
                break;
        case PROP_POLKIT_NO_TOOLTIP:
                g_value_set_string (value, action->priv->no_tooltip);
                break;
        case PROP_POLKIT_NO_ICON_NAME:
                g_value_set_string (value, action->priv->no_icon_name);
                break;

        case PROP_POLKIT_AUTH_VISIBLE:
                g_value_set_boolean (value, action->priv->auth_visible);
                break;
        case PROP_POLKIT_AUTH_SENSITIVE:
                g_value_set_boolean (value, action->priv->auth_sensitive);
                break;
        case PROP_POLKIT_AUTH_SHORT_LABEL:
                g_value_set_string (value, action->priv->auth_short_label);
                break;
        case PROP_POLKIT_AUTH_LABEL:
                g_value_set_string (value, action->priv->auth_label);
                break;
        case PROP_POLKIT_AUTH_TOOLTIP:
                g_value_set_string (value, action->priv->auth_tooltip);
                break;
        case PROP_POLKIT_AUTH_ICON_NAME:
                g_value_set_string (value, action->priv->auth_icon_name);
                break;

        case PROP_POLKIT_YES_VISIBLE:
                g_value_set_boolean (value, action->priv->yes_visible);
                break;
        case PROP_POLKIT_YES_SENSITIVE:
                g_value_set_boolean (value, action->priv->yes_sensitive);
                break;
        case PROP_POLKIT_YES_SHORT_LABEL:
                g_value_set_string (value, action->priv->yes_short_label);
                break;
        case PROP_POLKIT_YES_LABEL:
                g_value_set_string (value, action->priv->yes_label);
                break;
        case PROP_POLKIT_YES_TOOLTIP:
                g_value_set_string (value, action->priv->yes_tooltip);
                break;
        case PROP_POLKIT_YES_ICON_NAME:
                g_value_set_string (value, action->priv->yes_icon_name);
                break;

        case PROP_POLKIT_MASTER_VISIBLE:
                g_value_set_boolean (value, action->priv->master_visible);
                break;
        case PROP_POLKIT_MASTER_SENSITIVE:
                g_value_set_boolean (value, action->priv->master_sensitive);
                break;

        case PROP_POLKIT_TARGET_PID:
                g_value_set_uint (value, action->priv->target_pid);
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
set_property (GObject      *object,
	      guint         prop_id,
	      const GValue *value,
	      GParamSpec   *pspec)
{
        PolKitGnomeAction *action = POLKIT_GNOME_ACTION (object);
        
        switch (prop_id)
        {
        case PROP_POLKIT_ACTION_OBJ:
                polkit_gnome_action_set_polkit_action (action, g_value_get_pointer (value));
                break;

        case PROP_POLKIT_ACTION_OBJ_SUFFICIENT:
                polkit_gnome_action_set_polkit_action_sufficient (action, value);
                break;

        case PROP_POLKIT_SELF_BLOCKED_VISIBLE:
                polkit_gnome_action_set_self_blocked_visible (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_SELF_BLOCKED_SENSITIVE:
                polkit_gnome_action_set_self_blocked_sensitive (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_SELF_BLOCKED_SHORT_LABEL:
                polkit_gnome_action_set_self_blocked_short_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_SELF_BLOCKED_LABEL:
                polkit_gnome_action_set_self_blocked_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_SELF_BLOCKED_TOOLTIP:
                polkit_gnome_action_set_self_blocked_tooltip (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_SELF_BLOCKED_ICON_NAME:
                polkit_gnome_action_set_self_blocked_icon_name (action, g_value_get_string (value));
                break;

        case PROP_POLKIT_NO_VISIBLE:
                polkit_gnome_action_set_no_visible (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_NO_SENSITIVE:
                polkit_gnome_action_set_no_sensitive (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_NO_SHORT_LABEL:
                polkit_gnome_action_set_no_short_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_NO_LABEL:
                polkit_gnome_action_set_no_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_NO_TOOLTIP:
                polkit_gnome_action_set_no_tooltip (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_NO_ICON_NAME:
                polkit_gnome_action_set_no_icon_name (action, g_value_get_string (value));
                break;

        case PROP_POLKIT_AUTH_VISIBLE:
                polkit_gnome_action_set_auth_visible (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_AUTH_SENSITIVE:
                polkit_gnome_action_set_auth_sensitive (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_AUTH_SHORT_LABEL:
                polkit_gnome_action_set_auth_short_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_AUTH_LABEL:
                polkit_gnome_action_set_auth_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_AUTH_TOOLTIP:
                polkit_gnome_action_set_auth_tooltip (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_AUTH_ICON_NAME:
                polkit_gnome_action_set_auth_icon_name (action, g_value_get_string (value));
                break;

        case PROP_POLKIT_YES_VISIBLE:
                polkit_gnome_action_set_yes_visible (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_YES_SENSITIVE:
                polkit_gnome_action_set_yes_sensitive (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_YES_SHORT_LABEL:
                polkit_gnome_action_set_yes_short_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_YES_LABEL:
                polkit_gnome_action_set_yes_label (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_YES_TOOLTIP:
                polkit_gnome_action_set_yes_tooltip (action, g_value_get_string (value));
                break;
        case PROP_POLKIT_YES_ICON_NAME:
                polkit_gnome_action_set_yes_icon_name (action, g_value_get_string (value));
                break;

        case PROP_POLKIT_MASTER_VISIBLE:
                polkit_gnome_action_set_visible (action, g_value_get_boolean (value));
                break;
        case PROP_POLKIT_MASTER_SENSITIVE:
                polkit_gnome_action_set_sensitive (action, g_value_get_boolean (value));
                break;

        case PROP_POLKIT_TARGET_PID:
                polkit_gnome_action_set_target_pid (action, g_value_get_uint (value));
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static XID
_get_xid_from_proxy_widgets (PolKitGnomeAction *action)
{
        XID xid;
        GSList *i;
        GSList *proxies;

        /* unfortunately there's no way to get the proxy that caused
         * the ::activate signal.. so go through proxies and pick the
         * first one.. */

        /* TODO: this won't work for menus (mclasen) */

        xid = 0;

        proxies = gtk_action_get_proxies (GTK_ACTION (action));

        for (i = proxies; i != NULL; i = i->next) {
                GtkWidget *top_level;
                GtkWidget *proxy = i->data;

                top_level = gtk_widget_get_toplevel (proxy);

                if (top_level == NULL)
                        continue;

                if (! (GTK_WIDGET_TOPLEVEL (top_level) && GTK_IS_WINDOW (top_level)))
                        continue;

                if (top_level->window == NULL)
                        continue;

                xid = gdk_x11_drawable_get_xid (GDK_WINDOW (top_level->window));

                if (xid != 0)
                        break;
        }

        return xid;
}

static void
_show_dialog_cb (PolKitAction *pk_action, 
                 gboolean      gained_privilege, 
                 GError       *error, 
                 gpointer      user_data)
{
        PolKitGnomeAction *action = POLKIT_GNOME_ACTION (user_data);

        if (gained_privilege) {
                /* better make sure our local pk_result is up-to-date.. */
                _compute_polkit_result (action);

                //g_debug ("end auth, obtained it");

                /* now emit the 'activate' signal again.. */
                gtk_action_activate (GTK_ACTION (action));

        } else {
                //g_debug ("end auth, didn't obtain it");

                if (error != NULL) {
                        g_warning ("Caught error: %s", error->message);
                        g_error_free (error);
                }
        }

        g_signal_emit (action, signals [AUTH_END_SIGNAL], 0, gained_privilege);
}

static void 
_auth_start (PolKitGnomeAction *action)
{
        GError *error = NULL;

        //g_debug ("starting auth");
        if (!polkit_gnome_auth_obtain (action->priv->polkit_action, 
                                       (guint) _get_xid_from_proxy_widgets (action),
                                       (guint) _get_target_pid (action),
                                       _show_dialog_cb, 
                                       action, 
                                       &error)) {
                g_warning ("Caught error: %s", error->message);
                g_error_free (error);
        }
}

static void
polkit_gnome_action_activate (PolKitGnomeAction *action)
{
        switch (action->priv->pk_result) {
        case POLKIT_RESULT_YES:
                /* If PolicyKit says yes.. then let 'activate' signal
                 * propagate
                 */
                break;

        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_ONE_SHOT:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_SESSION:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_ALWAYS:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_ONE_SHOT:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_SESSION:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_ALWAYS:
                /* Otherwise, if the action needs auth..  stop the emission
                 * and start auth process.. 
                 */

                g_signal_stop_emission_by_name (action, "activate");

                if (action->priv->polkit_action != NULL) {
                        g_signal_emit (action, signals [AUTH_START_SIGNAL], 0);
                }
                break;

        default:
        case POLKIT_RESULT_NO:
                /* If PolicyKit says no... and we got here.. it means
                 * that the user set the property "no-sensitive" to
                 * TRUE.. Otherwise we couldn't be handling this signal.
                 *
                 * Hence, they probably have a good reason for doing
                 * this so do let the 'activate' signal propagate.. 
                 */
                break;
        }
}

static void
_update_tooltips (PolKitGnomeAction *action, GParamSpec *arg1, GtkWidget *widget)
{
        GtkTooltips *tips;
        GtkTooltipsData *ttd;
        gchar *tip_str;

        ttd = gtk_tooltips_data_get (widget);

        if (ttd == NULL) {
                tips = gtk_tooltips_new ();
        } else {
                tips = ttd->tooltips;
        }

        tip_str = NULL;
        g_object_get (action, "tooltip", &tip_str, NULL);

        /* TODO: if there is no tooltip the tip_str is NULL.
         * Unfortunately it seems that the tooltip isn't
         * cleared.. mmm.. gtk+ bug?
         */
        gtk_tooltips_set_tip (tips, widget, tip_str, tip_str);
        g_free (tip_str);
}

static void
_update_label (PolKitGnomeAction *action, GParamSpec *arg1, GtkWidget *widget)
{
        char *label;

        label = NULL;
        g_object_get (action, "label", &label, NULL);
        gtk_button_set_label (GTK_BUTTON (widget), label);
        g_free (label);
}

static void
_update_icon_name (PolKitGnomeAction *action, GParamSpec *arg1, GtkWidget *widget)
{
        gtk_button_set_image (GTK_BUTTON (widget), gtk_action_create_icon (GTK_ACTION (action), GTK_ICON_SIZE_BUTTON));
}

static void 
_button_clicked (GtkButton *button, PolKitGnomeAction *action)
{
        /* g_debug ("in _button_clicked"); */

        switch (action->priv->pk_result) {
        case POLKIT_RESULT_YES:
                break;

        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_ONE_SHOT:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_SESSION:
        case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_ALWAYS:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_ONE_SHOT:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_SESSION:
        case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_ALWAYS:
                /* g_debug ("blocking clicked"); */
                g_signal_stop_emission_by_name (button, "clicked");
                break;

        default:
        case POLKIT_RESULT_NO:
                break;
        }
}

static void 
_button_auth_end (PolKitGnomeAction *action, gboolean gained_privilege, GtkWidget *button)
{
        /* g_debug ("in _button_auth_end gained_privilege=%d", gained_privilege); */
        if (gained_privilege) {
                /* g_debug ("emitting clicked"); */
                gtk_action_block_activate_from (GTK_ACTION (action), button);
                g_signal_emit_by_name (button, "clicked");
                gtk_action_unblock_activate_from (GTK_ACTION (action), button);
        }
}

/**
 * polkit_gnome_action_create_button:
 * @action: The #PolKitGnomeAction object
 *
 * Create a button for the given action that displays the label,
 * tooltip and icon_name corresponding to whether the state, according
 * to PolicyKit, is no, auth or yes.
 * 
 * Returns: A #GtkButton instance connected to the action
 */
GtkWidget *
polkit_gnome_action_create_button (PolKitGnomeAction *action)
{
        GtkWidget *button;

        button = gtk_button_new ();

        gtk_action_connect_proxy (GTK_ACTION (action), button);

        _update_label (action, NULL, button);
        _update_tooltips (action, NULL, button);
        _update_icon_name (action, NULL, button);

        g_signal_connect (action, "notify::tooltip", G_CALLBACK (_update_tooltips), button);
        g_signal_connect (action, "notify::label", G_CALLBACK (_update_label), button);
        g_signal_connect (action, "notify::icon-name", G_CALLBACK (_update_icon_name), button);

        /* hook into the ::clicked signal and block it unless
         * PolicyKit says it's good to go. This is necessary when the
         * button is embedded in e.g. a GtkDialog since that class
         * hooks in ::clicked signals from GtkButton instances...
         *
         * Also, hook into ::auth_end signal from the
         * PolKitGnomeAction and synthesize ::clicked the signal if
         * the privilege was gained..
         */
        g_signal_connect (button, "clicked", G_CALLBACK (_button_clicked), action);
        g_signal_connect (action, "auth-end", G_CALLBACK (_button_auth_end), button);

        return button;
}