File: gtkcookie.c

package info (click to toggle)
gtkcookie 0.4-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 212 kB
  • ctags: 101
  • sloc: ansic: 1,498; makefile: 83; sh: 1
file content (2384 lines) | stat: -rw-r--r-- 83,246 bytes parent folder | download | duplicates (6)
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
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
/*

  gtkcookie.c --> gtk+ web browser cookie file editor

  Copyright (c) 1998 Manni Wood

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  Manni Wood: mwood@sig.bsh.com, pq1036@110.net

  Netscape, Netscape Navigator, Netscape ONE, and the Netscape N and
  Ship's Wheel logos are registered trademarks of Netscape Communications
  Corporation in the United States and other countries.
  
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>  /* uid_t */
#include <time.h>

#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>  /* contains all keyboard defs */
#include "proctool.h"
#include "getline.h"
#include "cookie.xpm"

#define COOKIE_COLUMNS 8

/*
  "The static declaration, applied to an external variable or function,
  limits the scope of that object to the rest of the source file being
  compiled."  -- K & R, p. 83
*/

int intcmp(const char *s1, const char *s2) {
    double v1, v2;

    v1 = atof(s1);
    v2 = atof(s2);

    if (v1 < v2) {
	return -1;
    } else if (v1 > v2) {
	return 1;
    } else {
	return 0;
    }
}

/* COOKIE FUNCTIONS
   A cookie is simply an array of pointers to gchar; each pointer to
   gchar holds the string value of one of the cookie fields:
    "Domain", - domain of the server that set the cookie
    "Suffix", - T/F: is the domain a suffix, or a full domain?
    "Path", - what path does the browser have to be in to return the cookie?
    "Secure", - T/F: is this cookie only sent over secure http?
    "Expire", - time in seconds, since the epoch, this cookie expires
    "Name", - name (or key) of the cookie
    "Value", - value of the cookie
    "Expire". - human-readable expiration date
    Please note that the final "Expire" is not kept in the cookie
    file, but is a human-readable version of the date held in the
    first "Expire".
*/

/* is a line of text from the cookie file a valid cookie? */
static gboolean is_a_cookie_line(gchar *line);

/* split a line of text into the seven cookie fields, and place
   in new_cookie */
static void split_cookie_line(gchar *line, gchar *new_cookie[COOKIE_COLUMNS]);

/* set a cookie's gchar pointers to NULL */
static void initialise_cookie(gchar *new_cookie[COOKIE_COLUMNS]);

/* free the memory pointed to by all the gchar pointers in a cookie,
   and set each gchar pointer to NULL */
static void free_cookie(gchar *c[COOKIE_COLUMNS]);

/* copy the first cookie's strings to the gchar ptrs of the second
   cookie, allocating the needed memory to do so */
static void copy_cookie(gchar *c1[COOKIE_COLUMNS], gchar *c2[COOKIE_COLUMNS]);

/* copy the cookie from a particular row in the clist to c,
   allocating the needed memory to do so */
static void copy_clist_row_to_cookie(gint row, gchar *c[COOKIE_COLUMNS]);

/* handy debugging function to print the contents of a cookie */
#ifdef _MANNI_DEBUG
static void dbg_print_cookie(gchar *c[COOKIE_COLUMNS]) {
    printf("domain: %s\n", c[0]);
    printf("suffix: %s\n", c[1]);
    printf("path: %s\n", c[2]);
    printf("secure: %s\n", c[3]);
    printf("expire: %s\n", c[4]);
    printf("name: %s\n", c[5]);
    printf("value: \"%s\"\n", c[6]);
    printf("expire: %s\n", c[7]);
}
#endif

/* CLIST FUNCTIONS
   Functions used to manipulate the list of cookies held in the
   clist in the main window.
*/

/* parse glbl_cookie_FILE and populate the main clist widget with the
   results */
static void populate_glbl_clist(void);

/* clear all items from main clist */
static void clear_glbl_clist(void);

/* remove a row from main clist */
static void remove_selected_row_from_glbl_clist(GtkWidget *calling_widget, gpointer func_data);

/* enumerate two sorting styles: sorting by string, and sorting
   by integer */
typedef enum { STRING, INTEGER } SORT_STYLE;

/* MANNI: the fact that I had to move the enum up here points to the fact
   that I prabably should move all global var declarations up above
   the global function declarations */

/* sort the clist using a quicksort */
static void quicksort_clist(GtkWidget *clist,
			    SORT_STYLE sort_style,
			    gint sort_column,
			    gint first_index,
			    gint last_index);

/* a utility function to help quicksort_clist work */
static void partition_clist(GtkWidget *clist,
			    SORT_STYLE sort_style,
			    gint sort_column,
			    gint first_index,
			    gint *pivot_index,
			    gint last_index);

/* swap the contents of two rows in the clist */
static void swap_row_clist(GtkWidget *clist, gint row1, gint row2);

/* respond to a double-click in the clist widget (by calling
 show_edit_dialog_box) */
static gboolean select_clist_dblclk(GtkWidget *clist,
			  GdkEventButton *event,
			  gpointer func_data);

/* copy the cookie from the selected row to the global variable which
   holds the currendly-selected cookie, glbl_selected_cookie */
static void select_clist (GtkWidget *clist,
		   gint row, 
		   gint column, 
		   GdkEventButton * bevent);

/* perform a sort on the column if its title button has been clicked */
/* PLEASE NOTE that sort_by_column_glbl_clist does not sort the clist
   arg, but sorts glbl_clist */
static void sort_by_column_glbl_clist (GtkWidget *clist,
			 gint column,
			 GdkEventButton * bevent);

/* inserts a new cookie in clist, and activates the edit dialog
 box for the new cookie.*/
static void insert_row_glbl_clist (GtkWidget *calling_widget, gpointer func_data);

/* creates the actual global clist widget */
static void create_glbl_clist (GtkWidget *box1);

/* copies the contents of glbl_selected_cookie into
   the correct row of the master clist (called by clicking
   "OK" on the cookie edit dialogue box), then destroys
   the edit dialogue box */
static void update_glbl_clist(GtkWidget *calling_widget,
			      GtkWidget *edit_win_to_destroy);

/* handle a keypress in the clist
   If <Del> was pressed, delete the currently selected cookie */
gint handle_keypress_in_main_win(GtkWidget *calling_widget, GdkEventKey *event, gpointer func_data);


/* COOKIE EDIT DIALOGUE BOX FUNCTIONS */

/* pops up the cookie edit dialogue box */
static void show_edit_dialog_box (GtkWidget *calling_widget, gpointer func_data);

/* whenever an editing change is made to any of the fields
 of a cookie, glbl_selected_cookie has memory re-allocated,
and strings re-copied, to reflect the changes made in
the dialogue box */
static void change_callback(GtkWidget *entry, gpointer func_data);

/* detects text changes in the human-readable date field of the
   cookie edit dialogue box, and changes the original expiry date
   field on the fly */
static void change_time_callback(GtkWidget *entry, gpointer func_data);

/* called when one of the true/false checkboxes in the cookie edit
   dialogue box changes state */
static void change_tf_callback(GtkWidget *toggle_button, gpointer func_data);

/* handle a keypress in the edit dialogue.
   If <Return> was pressed, destroy the window and save the cookie
   If <Esc> was pressed, destroy the find window and don't save the cookie. */
gint handle_keypress_in_edit_win(GtkWidget *edit_window, GdkEventKey *event, gpointer func_data);

/* FIND DIALOG BOX FUNCTIONS */

/* show the 'find' dialogue box */
static void show_find_dialog_box(GtkWidget *calling_widget, gpointer func_data);
static void search (GtkWidget*, gpointer);
static gint search_dlg_key_cb (GtkWidget*, GdkEventKey*, gpointer);
static void clear_search (GtkWidget*, gpointer);

/* make glbl_last_found_row equal -1 again */
static void reset_last_found_row(GtkWidget *calling_widget, gpointer func_data);

/* set case sensitive searching TRUE/FALSE */
/* (held in the global variable glbl_search_case_sensitive) */
static void set_tf_case_sensitive_search(GtkWidget *toggle_button, gpointer func_data);

/* OTHER DIALOG BOX FUNCTIONS */

/* show the 'about' dialogue box */
static void show_about_dialog_box(GtkWidget *calling_widget, gpointer func_data);

/* show an error dialogue box, with the following message */
static void show_error_dialog_box (gchar *message);

/* FILE SELECTION AND SAVE FUNCTIONS */

/* respond to the "OK" button in the open file dialogue */
static void file_selection_ok (GtkWidget *w, GtkFileSelection *fs);

/* respond to the "OK" button in the save_as dialogue box */
static void save_as_ok (GtkWidget *w, GtkFileSelection *fs);

/* creates the file selection dialogue box that you see when you use
   "open" */
static void show_open_file_dialog_box (GtkWidget *calling_widget, gpointer func_data);

/* creates the "save as" dialoge box */
static void show_save_as_dialog_box (GtkWidget *calling_widget, gpointer func_data);

/* handles the save_file request from the file manu; is smart enough
   to either call write_file() if the file exists, or call
   show_save_as_dialog_box if glbl_selected_filename_str is empty */
static void save_file(GtkWidget *calling_widget, gpointer func_data);

/* actually writes all of the cookie data held in the main clist
   out into a properly formatted mozilla file whose name
   should be held in glbl_selected_filename_str */
static void write_file(void);

/* OTHER FUNCTIONS */

/* check to see if there is a netscape lockfile for the current user,
   and if so, issue a warning */
static void check_for_running_netscape(void);

/* change the cursor -- seems not to be working */
/* static void set_cursor (guint c); */

/* ALL GLOBAL VARIABLES START WITH glbl_ */

/* cookie menu */
static GtkWidget *glbl_cookie_menu_item_holder;
static GtkWidget *glbl_cookie_menu_items;
static GtkWidget *glbl_cookie_menu;

/* the main programme window */
static GtkWidget *glbl_main_window;

/* title of the main programme window */
static GString *glbl_window_title_gstr;

/* the master combo list */
static GtkWidget *glbl_clist;

/* we need a global pointer to point to the expire time label in
   an edit window whenever the edit window is constructed. This will
   give us a handle to the label even after the function is run. We need
   the handle to the label so that we can change the value of the label
   as handler functions are called */
static GtkWidget *glbl_selected_etl;

/* we need a time struct to throw the results of strptime() into */
static struct tm glbl_selected_time;

/* we also need a time_t global var to convert glbl_selected_time from */
static time_t glbl_selected_time_sec;

/* finally, we need an ascii string representation of the
   glbl_selected_time_sec.
   January 2038 is when a 32 bit int will roll over, and that 32 bit int
   seems to need 10 ascii characters to print. I'll therefore allocate 12
   characters to be safe. This won't, however, be safe if we move to 64 bit
   ints */
static gchar glbl_selected_time_str[12];

static gint glbl_clist_rows = 0;
static gint glbl_clist_selected_row = 0;
static gchar *glbl_selected_cookie[COOKIE_COLUMNS]; /* the currently selected cookie in the glbl_clist */


/* glbl_selected_filename_str will be a string holding the name of the
   file from
   a file select box, or the default cookie file location */
static gchar *glbl_selected_filename_str = NULL;
static gchar *glbl_home_directory_str = NULL; /* user's home directory */
/* static gchar *glbl_netscape_dot_dir_str = NULL; netscape dot directory */
static FILE *glbl_cookie_FILE = NULL;
    
static gchar *glbl_cookie_titles_str[] = {
    "Domain",
    "Suffix",
    "Path",
    "Secure",
    "Expire (sec)",
    "Name",
    "Value",
    "Expire"
};

/* enumerate a label for each column name so that we can use
   a column name instead of a column number. cookie[Domain] tells
   us more than cookie[0] */
enum COLUMN_NAMES {COL_DOMAIN,
		   COL_SUFFIX,
		   COL_PATH,
		   COL_SECURE,
		   COL_EXPIRE_SECONDS,
		   COL_NAME,
		   COL_VALUE,
		   COL_EXPIRE_DATE};

/* sad, but true: a callback function that I attach to each text
   entry field in the cookie popup window requires a pointer to
   an int as an argument, so I have to maintain this array of pointers
   to ints just to keep track of which of the 8 text fields is being
   used. */
static gint *glbl_int_ptr[COOKIE_COLUMNS];

GdkPixmap *glbl_cookie_xpm;
GdkBitmap *glbl_cookie_xpm_mask;

/* keeps track of the row of the last successful find */
static gint glbl_last_found_row = -1;

/* keeps track of the last column that was sorted. If the value
   is -1, no column has been sorted */
static gint glbl_last_sorted_column = -1;

static gboolean glbl_search_case_sensitive = FALSE;

/*
  main
*/

int main (int argc, gchar *argv[]) {

    GtkWidget *file_menu_item_holder;
    GtkWidget *menu_bar;
    GtkWidget *file_menu;
    GtkWidget *menu_items;
    GtkWidget *vbox;

    GtkWidget *help_menu_item_holder;
    GtkWidget *help_menu_items;
    GtkWidget *help_menu;

    GtkWidget *edit_menu_item_holder;
    GtkWidget *edit_menu_items;
    GtkWidget *edit_menu;

    GdkColor *transparent = NULL;

	GtkAccelGroup* accel_group;

    int i;

    gtk_init (&argc, &argv);

    /* initialise the pointers to ints */
    for (i = 0; i < COOKIE_COLUMNS; i++) {
	glbl_int_ptr[i] = malloc(sizeof(int));
	*glbl_int_ptr[i] = i;
    }

    /* initialise the window title */
    glbl_window_title_gstr = g_string_new("gtkcookie");

    /* create a new glbl_main_windoindw */
    glbl_main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_usize( GTK_WIDGET (glbl_main_window), 700, 300);
    gtk_window_set_title(GTK_WINDOW (glbl_main_window), glbl_window_title_gstr->str);
    gtk_signal_connect(GTK_OBJECT (glbl_main_window), "delete_event",
		       GTK_SIGNAL_FUNC(gtk_main_quit),
		       NULL);
    
    /* handle any keypresses in the clist. For now, we only
       care about Del, but that could change. */
    gtk_signal_connect(GTK_OBJECT(glbl_main_window), "key_press_event",
		       GTK_SIGNAL_FUNC(handle_keypress_in_main_win),
		       NULL);

    /* add the icon to the glbl_main_window */

    gtk_widget_realize(glbl_main_window);
    
    glbl_cookie_xpm = gdk_pixmap_create_from_xpm_d (glbl_main_window->window,
						    &glbl_cookie_xpm_mask, 
						    transparent, 
						    cookiepic);

    gdk_window_set_icon(glbl_main_window->window, NULL,
			glbl_cookie_xpm, glbl_cookie_xpm);

	/* add accelerator_table to the window */
	accel_group = gtk_accel_group_new();
	gtk_window_add_accel_group(GTK_WINDOW(glbl_main_window), accel_group);

    /* FILE MENU */
    
    /* Init the menu-widget, and remember -- never
     * gtk_show_widget() the file_menu_item_holder widget!! 
     * This is the menu that holds the menu items, the one that
     * will pop up when you click on the "File" menu in the app */
    file_menu_item_holder = gtk_menu_new();
    
    /* Next we make the menu-entries for "file_menu_item_holder".
     * Notice the call to gtk_menu_append.  Here we are adding a list of
     * menu items to our menu.  Normally, we'd also catch the "clicked"
     * signal on each of the menu items and setup a callback for it,
     * but it's omitted here to save space. */

    /***************/
    
    /* Create a new menu-item with a name... */
    menu_items = gtk_menu_item_new_with_label("Open...");
	
    /* ...and add it to the file_menu_item_holder. */
    gtk_menu_append(GTK_MENU (file_menu_item_holder), menu_items);
	
    /* Do something interesting when the menuitem is selected */
    gtk_signal_connect_object(GTK_OBJECT(menu_items), "activate",
			      GTK_SIGNAL_FUNC(show_open_file_dialog_box), NULL);
	/* install the accelerator */
	gtk_widget_add_accelerator(menu_items, "activate", accel_group, 'O',
							   GDK_CONTROL_MASK, 0);

    /* Show the widget */
    gtk_widget_show(menu_items);

    /***************/

    /***************/
    
    /* Create a new menu-item with a name... */
    menu_items = gtk_menu_item_new_with_label("Save");
	
    /* ...and add it to the file_menu_item_holder. */
    gtk_menu_append(GTK_MENU (file_menu_item_holder), menu_items);
	
    /* Do something interesting when the menuitem is selected */
    gtk_signal_connect_object(GTK_OBJECT(menu_items), "activate",
			      GTK_SIGNAL_FUNC(save_file), NULL);
    
	/* install the accelerator */
	gtk_widget_add_accelerator(menu_items, "activate", accel_group, 'S',
							   GDK_CONTROL_MASK, 0);
	
    /* Show the widget */
    gtk_widget_show(menu_items);

    /***************/

    /***************/
    
    /* Create a new menu-item with a name... */
    menu_items = gtk_menu_item_new_with_label("Save as...");
	
    /* ...and add it to the file_menu_item_holder. */
    gtk_menu_append(GTK_MENU (file_menu_item_holder), menu_items);
	
    /* Do something interesting when the menuitem is selected */
    gtk_signal_connect_object(GTK_OBJECT(menu_items), "activate",
			      GTK_SIGNAL_FUNC(show_save_as_dialog_box), NULL);
	
    /* Show the widget */
    gtk_widget_show(menu_items);

    /***************/

    /***************/
    
    /* Create a new blank menu-item (which creates a line ... */
    menu_items = gtk_menu_item_new();
	
    /* ...and add it to the file_menu_item_holder. */
    gtk_menu_append(GTK_MENU(file_menu_item_holder), menu_items);

    /* no need to attach a function to a separator... */
	
    /* Show the widget */
    gtk_widget_show(menu_items);

    /***************/

    /***************/
    
    /* Create a new menu-item with a name... */
    menu_items = gtk_menu_item_new_with_label("Quit");
	
    /* ...and add it to the file_menu_item_holder. */
    gtk_menu_append(GTK_MENU (file_menu_item_holder), menu_items);
	
    /* quit when the "Exit" menuitem is selected */
    gtk_signal_connect(GTK_OBJECT (menu_items), "activate",
		       GTK_SIGNAL_FUNC(gtk_main_quit),
		       NULL);

	/* install the accelerator */
	gtk_widget_add_accelerator(menu_items, "activate", accel_group, 'Q',
							   GDK_CONTROL_MASK, 0);

    /* Show the widget */
    gtk_widget_show(menu_items);

    /***************/
    
    /* This is the file menu, and will be the label
     * displayed on the menu bar.  There won't be a signal handler attached,
     * as it only pops up the rest of the menu when pressed. */
    file_menu = gtk_menu_item_new_with_label("File");
    
    gtk_widget_show(file_menu);
    
    /* Now we specify that we want our newly created "file_menu_item_holder" to be the menu
     * for the "file menu" */
    gtk_menu_item_set_submenu(GTK_MENU_ITEM (file_menu), file_menu_item_holder);

    
    /* COOKIE MENU */
    
    glbl_cookie_menu_item_holder = gtk_menu_new();

    /********/
    glbl_cookie_menu_items = gtk_menu_item_new_with_label("New...");
	
    gtk_menu_append(GTK_MENU (glbl_cookie_menu_item_holder),
		    glbl_cookie_menu_items);
	
    gtk_signal_connect_object(GTK_OBJECT(glbl_cookie_menu_items), "activate",
			      GTK_SIGNAL_FUNC(insert_row_glbl_clist),
			      NULL);
	/* install the accelerator */
	gtk_widget_add_accelerator(glbl_cookie_menu_items, "activate", accel_group, 'N',
							   GDK_CONTROL_MASK, 0);

    gtk_widget_show(glbl_cookie_menu_items);
    /*********/

    /********/
    glbl_cookie_menu_items = gtk_menu_item_new_with_label("Edit...");
	
    gtk_menu_append(GTK_MENU (glbl_cookie_menu_item_holder),
		    glbl_cookie_menu_items);
	
    gtk_signal_connect_object(GTK_OBJECT(glbl_cookie_menu_items), "activate",
			      GTK_SIGNAL_FUNC(show_edit_dialog_box),
			      NULL);
	/* install the accelerator */
	gtk_widget_add_accelerator(glbl_cookie_menu_items, "activate", accel_group, 'E',
							   GDK_CONTROL_MASK, 0);

    gtk_widget_show(glbl_cookie_menu_items);
    /*********/

    /********/
    glbl_cookie_menu_items = gtk_menu_item_new_with_label("Delete");
	
    gtk_menu_append(GTK_MENU (glbl_cookie_menu_item_holder),
		    glbl_cookie_menu_items);
	
    gtk_signal_connect_object(GTK_OBJECT(glbl_cookie_menu_items), "activate",
			      GTK_SIGNAL_FUNC(remove_selected_row_from_glbl_clist),
			      NULL);
    /* add "Del" to this one. It won't work as a keyboard accelerator,
       so find another way */
	
    gtk_widget_show(glbl_cookie_menu_items);
    /*********/

    glbl_cookie_menu = gtk_menu_item_new_with_label("Cookie");

    gtk_widget_show(glbl_cookie_menu);
    
    gtk_menu_item_set_submenu(GTK_MENU_ITEM (glbl_cookie_menu),
			      glbl_cookie_menu_item_holder);

    /* HELP MENU */
    
    /* Init the menu-widget, and remember -- never
     * gtk_show_widget() the file_menu_item_holder widget!! 
     * This is the menu that holds the menu items, the one that
     * will pop up when you click on the "File" menu in the app */
    help_menu_item_holder = gtk_menu_new();
    
    /* Next we make the menu-entries for "file_menu_item_holder".
     * Notice the call to gtk_menu_append.  Here we are adding a list of
     * menu items to our menu.  Normally, we'd also catch the "clicked"
     * signal on each of the menu items and setup a callback for it,
     * but it's omitted here to save space. */

    /***************/
    
    /* Create a new menu-item with a name... */
    help_menu_items = gtk_menu_item_new_with_label("About...");
	
    /* ...and add it to the file_menu_item_holder. */
    gtk_menu_append(GTK_MENU (help_menu_item_holder), help_menu_items);
	
    /* Do something interesting when the menuitem is selected */
    gtk_signal_connect_object(GTK_OBJECT(help_menu_items), "activate",
			      GTK_SIGNAL_FUNC(show_about_dialog_box), NULL);
	
    /* Show the widget */
    gtk_widget_show(help_menu_items);

    /***************/

    /* This is the file menu, and will be the label
     * displayed on the menu bar.  There won't be a signal handler attached,
     * as it only pops up the rest of the menu when pressed. */
    help_menu = gtk_menu_item_new_with_label("Help");
    
    gtk_widget_show(help_menu);
    
    /* Now we specify that we want our newly created "file_menu_item_holder" to be the menu
     * for the "file menu" */
    gtk_menu_item_set_submenu(GTK_MENU_ITEM (help_menu), help_menu_item_holder);


    /* /HELP MENU */

    /* EDIT MENU */
    
    /* Init the menu-widget, and remember -- never
     * gtk_show_widget() the file_menu_item_holder widget!! 
     * This is the menu that holds the menu items, the one that
     * will pop up when you click on the "File" menu in the app */
    edit_menu_item_holder = gtk_menu_new();
    
    /* Next we make the menu-entries for "file_menu_item_holder".
     * Notice the call to gtk_menu_append.  Here we are adding a list of
     * menu items to our menu.  Normally, we'd also catch the "clicked"
     * signal on each of the menu items and setup a callback for it,
     * but it's omitted here to save space. */

    /***************/
    
    /* Create a new menu-item with a name... */
    edit_menu_items = gtk_menu_item_new_with_label("Find...");
	
    /* ...and add it to the file_menu_item_holder. */
    gtk_menu_append(GTK_MENU (edit_menu_item_holder), edit_menu_items);
	
    /* Do something interesting when the menuitem is selected */
    gtk_signal_connect_object(GTK_OBJECT(edit_menu_items), "activate",
			      GTK_SIGNAL_FUNC(show_find_dialog_box),
			      NULL);
    
	/* install the accelerator */
	gtk_widget_add_accelerator(edit_menu_items, "activate", accel_group, 'F',
							   GDK_CONTROL_MASK, 0);

    /* Show the widget */
    gtk_widget_show(edit_menu_items);

    /***************/

    /* This is the file menu, and will be the label
     * displayed on the menu bar.  There won't be a signal handler attached,
     * as it only pops up the rest of the menu when pressed. */
    edit_menu = gtk_menu_item_new_with_label("Edit");
    
    gtk_widget_show(edit_menu);
    
    /* Now we specify that we want our newly created "file_menu_item_holder" to be the menu
     * for the "file menu" */
    gtk_menu_item_set_submenu(GTK_MENU_ITEM (edit_menu), edit_menu_item_holder);


    /* /EDIT MENU */
    
    /* VBOX */
    
    /* A vbox to put a menu and a button in: */
    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(glbl_main_window), vbox);
    gtk_widget_show(vbox);
    
    /* Create a menu-bar to hold the menus and add it to our main glbl_main_window */
    menu_bar = gtk_menu_bar_new();
    gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 2);
    gtk_widget_show(menu_bar);
    
    /* embed the glbl_clist here */
    create_glbl_clist(vbox);

    /* And finally we append the menu-item to the menu-bar -- this is the
     * "File" menu-item I have been raving about =) */
    gtk_menu_bar_append(GTK_MENU_BAR (menu_bar), file_menu);
    gtk_menu_bar_append(GTK_MENU_BAR (menu_bar), edit_menu);
    gtk_menu_bar_append(GTK_MENU_BAR (menu_bar), glbl_cookie_menu);
    gtk_menu_bar_append(GTK_MENU_BAR (menu_bar), help_menu);

    /* always display the glbl_main_window as the last step
       so it all splashes on the screen at once. */
    gtk_widget_show(glbl_main_window);

    /* fill the glbl_clist widget with all of the cookies */
    populate_glbl_clist();

    /* after populating the clist, see if there is a netscape
       lock file, and be good enough to let the user know about it */
    check_for_running_netscape();

    gtk_main ();

    /* free the memory from the pointers to ints */
    for (i = 0; i < COOKIE_COLUMNS; i++) {
	free(glbl_int_ptr[i]);
    }


    return 0;
}


static void save_file(GtkWidget *calling_widget, gpointer func_data) {
    if (glbl_selected_filename_str == NULL) {
	/* show_save_as_dialog_box, when called as a callback,
	   takes a pointer to the calling widget, and a pointer
	   to extra function data, if required */
	show_save_as_dialog_box(NULL, NULL);
    } else {
	write_file();
    }

}

static void write_file (void) {
    GString *error_msg_gstr;
    gchar *cell_contents;
    int i;
    int row;

    /* set the title of the main window to show the selected filename */
    /* first, erase what's currently in the gstring */
    g_string_erase(glbl_window_title_gstr, 0, glbl_window_title_gstr->len);
    /* now create the new title, always starting with "gtkcookie - " and
       then the filename */
    g_string_assign(glbl_window_title_gstr, "gtkcookie - ");
    g_string_append(glbl_window_title_gstr, glbl_selected_filename_str);
    /* set the title of the main window */
    gtk_window_set_title(GTK_WINDOW (glbl_main_window), glbl_window_title_gstr->str);
    
    glbl_cookie_FILE = fopen(glbl_selected_filename_str, "w");
    if (glbl_cookie_FILE == NULL) {
	error_msg_gstr = g_string_new("");
	g_string_sprintf(error_msg_gstr, "Can't write the cookie file\n\"%s\".\n(Probably not allowed to write\nto this directory, or\nthe filesystem is full.)", glbl_selected_filename_str);
	show_error_dialog_box(error_msg_gstr->str);
	/* don't forget to deallocate the string after we're done
	   with it! Fortunately, deallocating the string seems not
	   to harm the error message created by show_error_dialog_box() */
	g_string_free(error_msg_gstr, TRUE);
	return;
    }

    fprintf(glbl_cookie_FILE, "# Netscape HTTP Cookie File\n");
    fprintf(glbl_cookie_FILE, "# http://www.netscape.com/newsref/std/cookie_spec.html\n");
    fprintf(glbl_cookie_FILE, "# This is a generated file!  Do not edit.\n\n");

    for (row = 0; row < glbl_clist_rows; row++) {
	for (i = 0; i < (COOKIE_COLUMNS - 1); i++) {
	    gtk_clist_get_text(GTK_CLIST (glbl_clist), row, i, &cell_contents);
	    if (i < (COOKIE_COLUMNS - 2)) {
		fprintf(glbl_cookie_FILE, "%s\t", cell_contents);
	    } else {
		fprintf(glbl_cookie_FILE, "%s\n", cell_contents);
	    }
	}
    }

    fclose(glbl_cookie_FILE);
}

static void show_about_dialog_box(GtkWidget *calling_widget, gpointer func_data) {
    static GtkWidget *about_window = NULL;
    GtkWidget *box1;
    GtkWidget *about_text_label;

	if (!about_window) {
		GtkWidget *btn;

		about_window = gtk_dialog_new();
		gtk_dialog_set_has_separator (GTK_DIALOG (about_window), FALSE);
	gtk_window_position(GTK_WINDOW(about_window), GTK_WIN_POS_MOUSE);

	gtk_signal_connect (GTK_OBJECT(about_window), "destroy",
			    GTK_SIGNAL_FUNC(gtk_widget_destroyed),
			    &about_window);

	gtk_window_set_title (GTK_WINDOW (about_window), "About gtkcookie");
	gtk_container_border_width (GTK_CONTAINER (about_window), 0);

		box1 = gtk_vbox_new (FALSE, 10);
		gtk_container_set_border_width (GTK_CONTAINER (box1), 10);
	gtk_container_add (GTK_CONTAINER (GTK_DIALOG (about_window)->vbox), box1);
	gtk_widget_show (box1);

	about_text_label = gtk_label_new("gtkcookie, version 0.03\n\nCode copyright (c) 1998 Manni Wood.\nCookie icons copyright (c) 1998 Glenn Matheson.\n\ngtkcookie comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome\nto redistribute it under certain conditions;\nfor details, see the file named 'COPYING' that\ncame with the source code of this programme.\n\nNetscape is a registered trademark of\nNetscape Communications Corporation\nin the United States and other countries.");
		gtk_box_pack_start (GTK_BOX (box1), about_text_label, TRUE, TRUE, 0);
	gtk_widget_show (about_text_label);

		btn = gtk_dialog_add_button (GTK_DIALOG (about_window), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
		g_signal_connect_swapped (G_OBJECT (btn), "clicked", G_CALLBACK (gtk_widget_destroy), G_OBJECT (about_window));
		gtk_widget_grab_default (btn);
		gtk_widget_show (btn);
	}

    if (!GTK_WIDGET_VISIBLE (about_window)) {
	gtk_widget_show (about_window);
    } else {
	gtk_widget_destroy (about_window);
    }
}

static void
show_find_dialog_box (GtkWidget* wdg, gpointer data) {
	static GtkWidget *search_dlg = NULL;
    GtkWidget *box1;
    GtkWidget *hbox2;
    GtkWidget *hbox1;

    GtkWidget *find_text_label;

    GtkWidget *case_sensitive_tf_checkbox;
    
    /* each time the find dialog box is opened, set glbl_last_found_row
       to zero, to indicate that whatever the user is about to search on,
       it is the first time he will be searching for that string */
    glbl_last_found_row = -1;

	if (!search_dlg) {
		GtkWidget *btn, *needle;

		search_dlg = gtk_dialog_new();
		gtk_window_position (GTK_WINDOW (search_dlg), GTK_WIN_POS_MOUSE);

		g_signal_connect (G_OBJECT (search_dlg), "destroy", G_CALLBACK (gtk_widget_destroyed), &search_dlg);
	/* REALLY IMPORTANT NOTE! gtk_widget_destroyed (notice the
	   past tense) seems to do something a whole lot different
	   than gtk_widget_destroy (notice the present tense)
	*/

		gtk_window_set_title (GTK_WINDOW (search_dlg), _("Find cookie"));

	box1 = gtk_vbox_new (FALSE, 0);
		gtk_container_add (GTK_CONTAINER (GTK_DIALOG (search_dlg)->vbox), box1);
	gtk_widget_show (box1);

	hbox1 = gtk_hbox_new (FALSE, 10);
	gtk_container_border_width (GTK_CONTAINER (hbox1), 10);
	gtk_box_pack_start (GTK_BOX (box1), hbox1, TRUE, TRUE, 0);
	gtk_widget_show (hbox1);


	find_text_label = gtk_label_new("Find:");
	gtk_box_pack_start (GTK_BOX (hbox1), find_text_label, TRUE, TRUE, 0);
	gtk_widget_show (find_text_label);

		needle = gtk_entry_new();
		g_signal_connect (G_OBJECT (needle), "changed", G_CALLBACK (reset_last_found_row), NULL);
		gtk_box_pack_start (GTK_BOX (hbox1), needle, TRUE, TRUE, 0);
		gtk_widget_grab_focus (needle);
		gtk_widget_show (needle);

	hbox2 = gtk_hbox_new (FALSE, 10);
	gtk_container_border_width (GTK_CONTAINER (hbox2), 10);
	gtk_box_pack_start (GTK_BOX (box1), hbox2, FALSE, TRUE, 0);
	gtk_widget_show (hbox2);


	case_sensitive_tf_checkbox = gtk_check_button_new_with_label ("Case sensitive");
 
	gtk_signal_connect(GTK_OBJECT(case_sensitive_tf_checkbox), "clicked",
			   GTK_SIGNAL_FUNC(set_tf_case_sensitive_search),
			   NULL);

	/* turn the button on or off depending on whether the
	   glbl_search_case_sensitive is "TRUE" or "FALSE" */
	if (glbl_search_case_sensitive == TRUE) {
	    /* checkbox is checked */
	    gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(case_sensitive_tf_checkbox),
					 TRUE );    
	} else {
	    /* checkbox is unchecked */
	    gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(case_sensitive_tf_checkbox),
					 FALSE );    
	}

	gtk_box_pack_start (GTK_BOX (hbox2), case_sensitive_tf_checkbox, FALSE, TRUE, 0);
	gtk_widget_show (case_sensitive_tf_checkbox);
	/********************/

		btn = gtk_dialog_add_button (GTK_DIALOG (search_dlg), GTK_STOCK_FIND, GTK_RESPONSE_ACCEPT);
		g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (search), needle);
		gtk_widget_show (btn);

		btn = gtk_dialog_add_button (GTK_DIALOG (search_dlg), GTK_STOCK_CLEAR, GTK_RESPONSE_REJECT);
		g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (clear_search), needle);
		gtk_widget_show (btn);

		btn = gtk_dialog_add_button (GTK_DIALOG (search_dlg), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
		g_signal_connect_swapped (G_OBJECT (btn), "clicked", G_CALLBACK (gtk_widget_destroy), G_OBJECT (search_dlg));
		gtk_widget_show (btn);

		/* check if the user hit Return and if so, call search() */
		g_signal_connect (G_OBJECT (search_dlg), "key_press_event", G_CALLBACK (search_dlg_key_cb), needle);
	}

	if (!GTK_WIDGET_VISIBLE (search_dlg)) {
		gtk_widget_show (search_dlg);
    } else {
		gtk_widget_destroy (search_dlg);
    }
}

static void show_error_dialog_box (gchar *message) {
    static GtkWidget *error_window = NULL;
    GtkWidget *box1;
    GtkWidget *about_text_label;

	if (!error_window) {
		GtkWidget *btn;

		error_window = gtk_dialog_new();
		gtk_dialog_set_has_separator (GTK_DIALOG (error_window), FALSE);
	gtk_window_position(GTK_WINDOW(error_window), GTK_WIN_POS_MOUSE);

	gtk_signal_connect (GTK_OBJECT (error_window), "destroy",
			    GTK_SIGNAL_FUNC(gtk_widget_destroyed),
			    &error_window);

	gtk_window_set_title (GTK_WINDOW (error_window), "Error");
	gtk_container_border_width (GTK_CONTAINER (error_window), 0);

		box1 = gtk_vbox_new (FALSE, 10);
		gtk_container_border_width (GTK_CONTAINER (box1), 10);
		gtk_container_add (GTK_CONTAINER (GTK_DIALOG (error_window)->vbox), box1);
	gtk_widget_show (box1);

	about_text_label = gtk_label_new(message);
		gtk_box_pack_start (GTK_BOX (box1), about_text_label, TRUE, TRUE, 0);
	gtk_widget_show (about_text_label);

		btn = gtk_dialog_add_button (GTK_DIALOG (error_window), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
		g_signal_connect_swapped (G_OBJECT (btn), "clicked", G_CALLBACK (gtk_widget_destroy), G_OBJECT (error_window));
		gtk_widget_grab_default (btn);
		gtk_widget_show (btn);
	}

    if (!GTK_WIDGET_VISIBLE (error_window)) {
	/* gtk_widget_realize (error_window); */
	gtk_grab_add (error_window);
	gtk_widget_show (error_window);
	/* gdk_window_raise (error_window->window); */
    } else {
	gtk_grab_remove (error_window);
	gtk_widget_destroy (error_window);
    }
}


static void clear_glbl_clist(void) {
    gtk_clist_clear (GTK_CLIST (glbl_clist));
    glbl_clist_rows = 0;
}

static void remove_selected_row_from_glbl_clist(GtkWidget *calling_widget, gpointer func_data) {
    gtk_clist_remove (GTK_CLIST (glbl_clist), glbl_clist_selected_row);
    glbl_clist_rows--;
}

static void sort_by_column_glbl_clist (GtkWidget *clist,
			 gint sort_column, 
			 GdkEventButton * bevent) {

    /* if the sort_column is the human readable date,
       make sort_comumn equal the numeric date column,
       because that's what we really want to sort the date on
       anyway. */
    if (sort_column == COL_EXPIRE_DATE) {
	sort_column = COL_EXPIRE_SECONDS;
    }
    
    /* as a small but useful optimisation, let us check to see
       if the sort_column we are about to sort on has already been
       sorted. If so, we can return right now. */
    if (sort_column == glbl_last_sorted_column) {
	return;
    }

    /* find a way to make the icon a watch */
    /* set_cursor(150); seems not to be working */
    /* I freeze graphical updates to the clist while the sort takes place */

    gtk_clist_freeze(GTK_CLIST (glbl_clist));

    if (sort_column == COL_EXPIRE_SECONDS) {
	quicksort_clist(glbl_clist, INTEGER, sort_column, 0, (glbl_clist_rows - 1));
    } else {
	quicksort_clist(glbl_clist, STRING, sort_column, 0, (glbl_clist_rows - 1));
    }

    gtk_clist_thaw(GTK_CLIST (glbl_clist));
    /* find a way to turn the icon from a watch to a pointer */
    /* set_cursor(132); seems not to be working */
    glbl_last_sorted_column = sort_column;

    /* finally, make sure the selected and highlighted cookie
       is the first one! */
    select_clist(glbl_clist, 0, 0, NULL);
}

static void swap_row_clist(GtkWidget *clist, gint row1, gint row2) {
    gchar *tmp_cookie[COOKIE_COLUMNS];
    gchar *cell_contents;
    gint col;

    initialise_cookie(tmp_cookie);  /* always do this with a new cookie! */

    /* if row1 and row2 are the same, then there is no work to be done */
    if (row1 == row2) {
	return;
    }

    copy_clist_row_to_cookie(row1, tmp_cookie);

    for (col = 0; col < COOKIE_COLUMNS; col++) {
	gtk_clist_get_text(GTK_CLIST (clist), row2, col,
			   &cell_contents);
	gtk_clist_set_text(GTK_CLIST (clist), row1, col,
			   cell_contents);
    }
    for (col = 0; col < COOKIE_COLUMNS; col++) {
	gtk_clist_set_text(GTK_CLIST (clist), row2, col,
			   tmp_cookie[col]);
    }
    
    /* free the memory used by the cookie before leaving subroutine! */
    free_cookie(tmp_cookie);
}

static gboolean select_clist_dblclk(GtkWidget *clist,
		  GdkEventButton *event,
		  gpointer func_data) {

    gint row;

    row = glbl_clist_selected_row;
    
    if (GTK_IS_CLIST(clist) && event->type == GDK_2BUTTON_PRESS) {
	show_edit_dialog_box(clist, func_data);
    }

    return FALSE;
}

static void select_clist (GtkWidget *clist,
	 	   gint row, 
		   gint column, 
		   GdkEventButton * bevent) {
    
    gchar *tmp_cookie[COOKIE_COLUMNS];
    gint i;

    /* initialise all of glbl_selected_cookie's gchar pointers to NULL */
    initialise_cookie(tmp_cookie);
    
    for (i = 0; i < COOKIE_COLUMNS; i++) {
	switch (gtk_clist_get_cell_type (GTK_CLIST (clist), row, i)) {
	case GTK_CELL_TEXT:
	    gtk_clist_get_text(GTK_CLIST (clist), row, i, &tmp_cookie[i]);
	    break;

	default:
	    break;
	}
    }

    /* tmp_cookie contains pointers to the strings in the clist,
       but I need a copy of the cookie whose contents I can modify
       without messing with the text in the glbl_clist. Hence, I copy
       tmp_cookie's strings to the global var, glbl_selected_cookie */
    copy_cookie(tmp_cookie, glbl_selected_cookie);

    /* IMPORTANT! set the selected row to the selected row */
    glbl_clist_selected_row = row;

    /* if right-clicked (ie, button 3), pop up the cookie menu */
    /* note that I'm checking if bevent is NULL, because sometimes
       I call this function directly (instead if it being called by
       an event) and therefore just make the last argument (bevent) NULL.
       Checking bevent->button on NULL crashes the programme. */
    if (bevent != NULL && bevent->button == 3) {
	gtk_menu_popup (GTK_MENU(glbl_cookie_menu_item_holder),
			NULL, NULL, NULL, NULL,
			bevent->button, bevent->time);
    }

}


static void insert_row_glbl_clist (GtkWidget *calling_widget, gpointer func_data) {
    /* future:
       1. clear the currently selected cookie
       2. gtk_clist_insert the currently selected cookie
       3. make the just-inserted cookie the selected cookie in the
       glbl_clist (ensure all of the global "this is the selected cookie row"
       variables reflect this change)
       4. scroll the glbl_clist so that the new, selected cookie is visible
       5. call the double-click function that brings up the cookie edit
       window */
    static gchar *text[] = {
	"url.com",
	"TRUE",
	"/",
	"FALSE",
	"0",
	"key",
	"value",
	"Thu Jan  1 00:00:00 1970"
    };

    gtk_clist_insert (GTK_CLIST(glbl_clist), glbl_clist_selected_row, text);
    glbl_clist_rows++;
    glbl_last_sorted_column = -1;  /* because we just added
				      a new row, no columns
				      can be assumed to be
				      sorted */
    gtk_clist_select_row(GTK_CLIST(glbl_clist),
                                 glbl_clist_selected_row,
                                 0 );
    
    show_edit_dialog_box(calling_widget, func_data);
}


static void create_glbl_clist (GtkWidget *box1) {
	/* ScrolledWindow to be put around glbl_clist */
	GtkWidget* sw_clist;

    /* create GtkCList here so we have a pointer to throw at the 
     * button callbacks -- more is done with it later */
    glbl_clist = gtk_clist_new_with_titles (COOKIE_COLUMNS, glbl_cookie_titles_str);

    /* 
     * the rest of the glbl_clist configuration
     */
    gtk_clist_set_row_height (GTK_CLIST (glbl_clist), 20);
    /* 20 pixels high for a single row? */


    /* if the user clicks on a column title button... */
    gtk_signal_connect (GTK_OBJECT (glbl_clist),
			"click_column",
			GTK_SIGNAL_FUNC(sort_by_column_glbl_clist),
			NULL);
    
    /* if the user clicks on a row... */  
    gtk_signal_connect (GTK_OBJECT (glbl_clist), 
			"select_row",
			GTK_SIGNAL_FUNC(select_clist), 
			NULL);


    /* if the user double-clicks on a row... */  
    gtk_signal_connect(GTK_OBJECT(glbl_clist),
		       "button_press_event",
		       GTK_SIGNAL_FUNC(select_clist_dblclk),
		       NULL);


    /* set the width and justification of each cookie column */
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_DOMAIN, 100);
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_SUFFIX, 50);
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_PATH, 50);
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_SECURE, 50);
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_EXPIRE_SECONDS, 80);
    gtk_clist_set_column_justification(GTK_CLIST(glbl_clist), COL_EXPIRE_SECONDS, GTK_JUSTIFY_RIGHT);
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_NAME, 80);
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_VALUE, 80);
    gtk_clist_set_column_width(GTK_CLIST(glbl_clist), COL_EXPIRE_DATE, 150);
    /* WIDTH HEIGHT */
    /* gtk_widget_set_usize(glbl_clist, width, height); */
    /* but first check to see if there is a window width/height */

    /* what is a selection mode? */
    gtk_clist_set_selection_mode (GTK_CLIST (glbl_clist), GTK_SELECTION_BROWSE);
	/* what is a policy? */
	sw_clist = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw_clist), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(sw_clist), glbl_clist);

	gtk_container_border_width (GTK_CONTAINER (glbl_clist), 0);
	gtk_box_pack_start (GTK_BOX (box1), sw_clist, TRUE, TRUE, 0);
	gtk_widget_show (glbl_clist);
	gtk_widget_show(sw_clist);
}

/* POPULATE GLBL_CLIST */

static void populate_glbl_clist(void) {
    gchar *file_line = NULL;

    /* were we successful reading a line from the file? */
    gboolean success = FALSE;
    
    gchar *test_cookie[COOKIE_COLUMNS];
    GString *error_msg_gstr;
    size_t pathname_str_length;

    glbl_clist_rows = 0;  /* seeing as we are starting fresh, the
			total number of rows in the glbl_clist widget is 0 */

    glbl_last_sorted_column = -1;  /* also, because we
				      are starting fresh,
				      the last sorted column
				      becomes unknown (-1)
				      again */

    /* set all gchar pointers in test_cookie to zero */
    initialise_cookie(test_cookie);

    /* if the selected filename is NULL, then we look
       in the default location for the cookie file */
    if (glbl_selected_filename_str == NULL) {
	glbl_home_directory_str = getenv("HOME");
	if (glbl_home_directory_str == NULL) {
	    show_error_dialog_box ("Environmental variable HOME not set;\ntherefore, can't find home directory\nor .netscape directory.");
	    return;
	}
	pathname_str_length = strlen(glbl_home_directory_str) + strlen("/.netscape/cookies");
	glbl_selected_filename_str = calloc(pathname_str_length + 1, sizeof(gchar));
	strncpy(glbl_selected_filename_str, glbl_home_directory_str, strlen(glbl_home_directory_str));
	strcat(glbl_selected_filename_str, "/.netscape/cookies");
	glbl_selected_filename_str[pathname_str_length] = '\0';
    }

    /* set the title of the main window to show the selected filename */
    /* first, erase what's currently in the gstring */
    g_string_erase(glbl_window_title_gstr, 0, glbl_window_title_gstr->len);
    /* now create the new title, always starting with "gtkcookie - " and
       then the filename */
    g_string_assign(glbl_window_title_gstr, "gtkcookie - ");
    g_string_append(glbl_window_title_gstr, glbl_selected_filename_str);
    /* set the title of the main window */
    gtk_window_set_title(GTK_WINDOW (glbl_main_window), glbl_window_title_gstr->str);

    glbl_cookie_FILE = fopen(glbl_selected_filename_str, "r");
    if (glbl_cookie_FILE == NULL) {
	/* if we couldn't open the cookie file, set
	   glbl_selected_filename_str to
	   NULL so that if the user says 'save', a 'save as' will be
	   invoked instead. */
	if (glbl_selected_filename_str != NULL) {
	    free(glbl_selected_filename_str);
	    glbl_selected_filename_str = NULL;
	}
	error_msg_gstr = g_string_new("");
	g_string_sprintf(error_msg_gstr, "can't open the cookie file\n\"%s\"\nfor reading.", glbl_selected_filename_str);
	show_error_dialog_box(error_msg_gstr->str);
	/* DEALLOCATE! */
	g_string_free(error_msg_gstr, TRUE);
	return;
    }

    file_line = get_line_safely(glbl_cookie_FILE, &success);
    if (success && is_a_cookie_line(file_line)) {
	split_cookie_line(file_line, test_cookie);
	gtk_clist_append(GTK_CLIST(glbl_clist), test_cookie);
	glbl_clist_rows++;  /* do this whenever you gtk_clist_append() */
	free_cookie(test_cookie);
    }
    free(file_line);
    file_line = NULL;

    while (success) {
	file_line = get_line_safely(glbl_cookie_FILE, &success);
	if (success && is_a_cookie_line(file_line)) {
	    split_cookie_line(file_line, test_cookie);
	    gtk_clist_append(GTK_CLIST(glbl_clist), test_cookie);
	    glbl_clist_rows++;  /* do this whenever you gtk_clist_append() */
	    free_cookie(test_cookie);
	}
	free(file_line);
	file_line = NULL;
    }

    fclose(glbl_cookie_FILE);
}

/* COOKIE FUNCTIONS */

static gboolean is_a_cookie_line(gchar *line) {
    /* a valid cookie line has 6 tabs */
    int tab_count;
    size_t i;

    /* always deal with the possibility of a null string! */
    if (line == NULL) {
	return FALSE;
    }
    
    tab_count = 0;
    for (i = 0; i < strlen(line); i++) {
	if (line[i] == '\t') {
	    tab_count++;
	}
    }

    if (tab_count == 6) {
	return TRUE;
    } else {
	return FALSE;
    }
}

/* make sure each pointer to gchar in new_cookie points to
   zero. This will make free_cookie() work better, because
   it will only free non-zero pointers */
static void initialise_cookie(gchar *new_cookie[COOKIE_COLUMNS]) {
    int i;
    for (i = 0; i < COOKIE_COLUMNS; i++) {
	new_cookie[i] = NULL;
    }
}

static void split_cookie_line(gchar *line, gchar *new_cookie[COOKIE_COLUMNS]) {
    /* from the string 'line', all the following ints
       will hold the start position and length of each cookie part
    */
    gchar *line_ptr;  /* pointer that will be incremented along a string */
    size_t domain_start, domain_length;
    size_t suffix_start, suffix_length;
    size_t path_start, path_length;
    size_t secure_start, secure_length;
    size_t expire_start, expire_length;
    size_t name_start, name_length;
    size_t value_start, value_length;

    size_t tab_locations[6];
    size_t number_of_tabs_found;

    size_t line_length; /* length of 'line' not including terminating '\0' */
    size_t i;  /* iterator */

    time_t cookie_time;
    struct tm *cookie_tm_ptr;

    /* make line_ptr and line equal. Later, line_ptr will get
       incremented along the string that line points to */
    line_ptr = line;

    line_length = strlen(line);

    number_of_tabs_found = 0;

    for (i = 0; i <= line_length; i++) {
	if (line[i] == '\t') {
	    tab_locations[number_of_tabs_found] = i;
	    number_of_tabs_found++;
	    /* DON'T even think about trying to assign an extra tab
	       past the bounds of the array! So if there are too many
	       tabs, break out of the loop! */
	    if (number_of_tabs_found >= 6) {
		break;
	    }
	}
    }

    /* enum COLUMN_NAMES {COL_DOMAIN,
		   COL_SUFFIX,
		   COL_PATH,
		   COL_SECURE,
		   COL_EXPIRE_SECONDS,
		   COL_NAME,
		   COL_VALUE,
		   COL_EXPIRE_DATE};*/

    domain_start = 0;
    suffix_start = tab_locations[0] + 1;
    path_start = tab_locations[1] + 1;
    secure_start = tab_locations[2] + 1;
    expire_start = tab_locations[3] + 1;
    name_start = tab_locations[4] + 1;
    value_start = tab_locations[5] + 1;

    domain_length = suffix_start - domain_start - 1;
    suffix_length = path_start - suffix_start - 1;
    path_length = secure_start - path_start - 1;
    secure_length = expire_start - secure_start - 1;
    expire_length = name_start - expire_start - 1;
    name_length = value_start - name_start - 1;
    value_length = line_length - value_start - 1;

    new_cookie[COL_DOMAIN] = calloc(domain_length + 1, sizeof(gchar));
    strncpy(new_cookie[COL_DOMAIN], line_ptr, domain_length);
    new_cookie[COL_DOMAIN][domain_length] = '\0';

    /* increment line_ptr so that it points to a shorter line */
    line_ptr += (domain_length + 1);
    
    new_cookie[COL_SUFFIX] = calloc(suffix_length + 1, sizeof(gchar));
    strncpy(new_cookie[COL_SUFFIX], line_ptr, suffix_length);
    new_cookie[COL_SUFFIX][suffix_length] = '\0';

    line_ptr += (suffix_length + 1);

    new_cookie[COL_PATH] = calloc(path_length + 1, sizeof(gchar));
    strncpy(new_cookie[COL_PATH], line_ptr, path_length);
    new_cookie[COL_PATH][path_length] = '\0';


    line_ptr += (path_length + 1);

    new_cookie[COL_SECURE] = calloc(secure_length + 1, sizeof(gchar));
    strncpy(new_cookie[COL_SECURE], line_ptr, secure_length);
    new_cookie[COL_SECURE][secure_length] = '\0';


    line_ptr += (secure_length + 1);

    new_cookie[COL_EXPIRE_SECONDS] = calloc(expire_length + 1, sizeof(gchar));
    strncpy(new_cookie[COL_EXPIRE_SECONDS], line_ptr, expire_length);
    new_cookie[COL_EXPIRE_SECONDS][expire_length] = '\0';


    line_ptr += (expire_length + 1);

    new_cookie[COL_NAME] = calloc(name_length + 1, sizeof(gchar));
    strncpy(new_cookie[COL_NAME], line_ptr, name_length);
    new_cookie[COL_NAME][name_length] = '\0';

    line_ptr += (name_length + 1);

    /* OK, this is a bit scary, but for some reason,
       we have to go one extra char here... otherwise,
       we lose the last char of the value! (which is the
       last char of the line, not counting the '\0') */
    new_cookie[COL_VALUE] = calloc(value_length + 2, sizeof(gchar));
    strncpy(new_cookie[COL_VALUE], line_ptr, value_length + 1);
    new_cookie[COL_VALUE][value_length + 1] = '\0';


    /* a human-readable date is created and placed in slot 7 */
    cookie_time = atoi(new_cookie[COL_EXPIRE_SECONDS]);
    /* cookie_tm_ptr = malloc(sizeof(struct tm)); seems unnecessary */
    cookie_tm_ptr = gmtime(&cookie_time);
    /* interestingly, asctime() always returns a string 26 gchars
       long, including a '\n' and a '\0' at the end. I think I'll
       turn the '\n' into a ' ' */
    new_cookie[COL_EXPIRE_DATE] = calloc(26, sizeof(gchar));
    strncpy(new_cookie[COL_EXPIRE_DATE], asctime(cookie_tm_ptr), 26);
    /* change the '\n' at slot 24 to a ' ' */
    new_cookie[COL_EXPIRE_DATE][24] = ' ';
    /* free(cookie_tm_ptr);  causes SIGSEGV */
}

static void free_cookie(gchar *c[COOKIE_COLUMNS]) {
    int i;
    for (i = 0; i < COOKIE_COLUMNS; i++) {
	if (c[i] != NULL) {
	    free(c[i]);
	    c[i] = NULL;
	}
    }
}

static void copy_cookie(gchar *c1[COOKIE_COLUMNS], gchar *c2[COOKIE_COLUMNS]) {
    /* COPIES c1 to c2 */
    
    /* ASSUMPTION: c1 and c2's pointers are all initialised, either to NULL
       or to a string of gchar */

    int i;
    size_t field_length;

    /* if c2 was pointing to anything, it won't be for long... */
    /* Remember: free_cookie frees any memory pointed to by c2
       as well as setting all of c2's pointers to null */
    free_cookie(c2);

    for (i = 0; i < COOKIE_COLUMNS; i++) {
	if (c1[i] != NULL) {
	    field_length = strlen(c1[i]);
	    c2[i] = calloc(field_length + 1, sizeof(gchar));
	    strncpy(c2[i], c1[i], field_length);
	    c2[i][field_length] = '\0';
	}
    }
    
}


static void copy_clist_row_to_cookie(gint row, gchar *c[COOKIE_COLUMNS]) {
    int i;
    size_t field_length;
    gchar *cell_contents;

    /* if c was pointing to anything, it won't be for long... */
    /* Remember: free_cookie frees any memory pointed to by c
       as well as setting all of c's pointers to null */
    free_cookie(c);

    /* if the row is larger than the number of rows in the clist,
       we can return right now */
    if (row >= glbl_clist_rows) {
	return;
    }

    for (i = 0; i < COOKIE_COLUMNS; i++) {
	gtk_clist_get_text(GTK_CLIST (glbl_clist), row, i, &cell_contents);
	if (cell_contents != NULL) {
	    field_length = strlen(cell_contents);
	    c[i] = calloc(field_length + 1, sizeof(gchar));
	    strncpy(c[i], cell_contents, field_length);
	    c[i][field_length] = '\0';
	}
    }
}


/*
 * GtkEntry
 */

static void show_edit_dialog_box (GtkWidget *calling_widget, gpointer func_data) {
    static GtkWidget *window = NULL;
    GtkWidget *box1;
    GtkWidget *box2;
    GtkWidget *table;
    GtkWidget *hbox;
    GtkWidget *text_entry[COOKIE_COLUMNS];

    GtkWidget *suffix_tf_checkbox, *secure_tf_checkbox;
    
    GtkWidget *OK_button;
    GtkWidget *Cancel_button;
    GtkWidget *separator;
    GtkWidget *label, *exp_s_label;
    gint row;
    row = glbl_clist_selected_row;

    if (!window) {
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	gtk_signal_connect (GTK_OBJECT (window), "destroy",
			    GTK_SIGNAL_FUNC(gtk_widget_destroyed),
			    &window);
	/* any keypress over this window calls handle_keypress_in_edit_win
	   so that when an <Enter> or <Esc> is detected, it calls what
	   the OK and Cancel buttons would do respectively */
	gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
			   GTK_SIGNAL_FUNC(handle_keypress_in_edit_win),
			   NULL);

	gtk_window_set_title (GTK_WINDOW (window), "Edit Cookie");
	gtk_container_border_width (GTK_CONTAINER (window), 0);


	box1 = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (window), box1);
	gtk_widget_show (box1);


	box2 = gtk_vbox_new (FALSE, 10);
	gtk_container_border_width (GTK_CONTAINER (box2), 10);
	gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
	gtk_widget_show (box2);

	table = gtk_table_new(9,
			      2,
                              FALSE);
	gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);

/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */

	/* COL_DOMAIN Name Edit Box */
	label = gtk_label_new(glbl_cookie_titles_str[COL_DOMAIN]);
 
	text_entry[COL_DOMAIN] = gtk_entry_new ();
 
	    /* I need to pass a pointer to an int as an argument for the
	       signal connect function. The int represents the
	       column of the bit of cookie data. That's what the
	       glbl_int_ptr[COL_DOMAIN] is for.
	       */
	
	gtk_signal_connect(GTK_OBJECT(text_entry[COL_DOMAIN]), "changed",
			   GTK_SIGNAL_FUNC(change_callback),
			   glbl_int_ptr[COL_DOMAIN]);
	gtk_entry_set_text (GTK_ENTRY (text_entry[COL_DOMAIN]),
			    glbl_selected_cookie[COL_DOMAIN]);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
	gtk_table_attach_defaults(GTK_TABLE(table), text_entry[COL_DOMAIN], 1, 2, 0, 1);
	gtk_widget_show (label);
	gtk_widget_show (text_entry[COL_DOMAIN]);


/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */
	/* Name Edit Box */
	label = gtk_label_new(glbl_cookie_titles_str[COL_NAME]);
 
	text_entry[COL_NAME] = gtk_entry_new ();
 
	gtk_signal_connect(GTK_OBJECT(text_entry[COL_NAME]), "changed",
			   GTK_SIGNAL_FUNC(change_callback),
			   glbl_int_ptr[COL_NAME]);
	gtk_entry_set_text (GTK_ENTRY (text_entry[COL_NAME]),
			    glbl_selected_cookie[COL_NAME]);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
	gtk_table_attach_defaults(GTK_TABLE(table), text_entry[COL_NAME], 1, 2, 1, 2);
	gtk_widget_show (label);
	gtk_widget_show (text_entry[COL_NAME]);


/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */
	/* Value Edit Box */
	label = gtk_label_new(glbl_cookie_titles_str[COL_VALUE]);
 
	text_entry[COL_VALUE] = gtk_entry_new ();
 
	gtk_signal_connect(GTK_OBJECT(text_entry[COL_VALUE]), "changed",
			   GTK_SIGNAL_FUNC(change_callback),
			   glbl_int_ptr[COL_VALUE]);
	gtk_entry_set_text (GTK_ENTRY (text_entry[COL_VALUE]),
			    glbl_selected_cookie[COL_VALUE]);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
	gtk_table_attach_defaults(GTK_TABLE(table), text_entry[COL_VALUE], 1, 2, 2, 3);
	gtk_widget_show (label);
	gtk_widget_show (text_entry[COL_VALUE]);


/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */
	/* COL_PATH Edit Box */
	label = gtk_label_new(glbl_cookie_titles_str[COL_PATH]);
 
	text_entry[COL_PATH] = gtk_entry_new ();
 
	gtk_signal_connect(GTK_OBJECT(text_entry[COL_PATH]), "changed",
			   GTK_SIGNAL_FUNC(change_callback),
			   glbl_int_ptr[COL_PATH]);
	gtk_entry_set_text (GTK_ENTRY (text_entry[COL_PATH]),
			    glbl_selected_cookie[COL_PATH]);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
	gtk_table_attach_defaults(GTK_TABLE(table), text_entry[COL_PATH], 1, 2, 3, 4);
	gtk_widget_show (label);
	gtk_widget_show (text_entry[COL_PATH]);
	

/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */
	/* COL_SUFFIX T/F Edit Box */

	suffix_tf_checkbox = gtk_check_button_new_with_label ("Domain field is actually a suffix");
 
	gtk_signal_connect(GTK_OBJECT(suffix_tf_checkbox), "clicked",
			   GTK_SIGNAL_FUNC(change_tf_callback),
			   glbl_int_ptr[COL_SUFFIX]);

	/* turn the button on or off depending on whether the
	   cookie field is "TRUE" or "FALSE" */
	if (strcmp(glbl_selected_cookie[COL_SUFFIX], "TRUE") == 0) {
	    /* checkbox is checked */
	    gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(suffix_tf_checkbox),
					 TRUE );    
	} else {
	    /* checkbox is unchecked */
	    gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(suffix_tf_checkbox),
					 FALSE );    
	}

	gtk_table_attach_defaults(GTK_TABLE(table), suffix_tf_checkbox, 0, 2, 4, 5);
	
	gtk_widget_show (suffix_tf_checkbox);

/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */
	/* COL_SECURE T/F Edit Box */

	secure_tf_checkbox = gtk_check_button_new_with_label ("Make this cookie secure");
 
	gtk_signal_connect(GTK_OBJECT(secure_tf_checkbox), "clicked",
			   GTK_SIGNAL_FUNC(change_tf_callback),
			   glbl_int_ptr[COL_SECURE]);

	/* turn the button on or off depending on whether the
	   cookie field is "TRUE" or "FALSE" */
	if (strcmp(glbl_selected_cookie[COL_SECURE], "TRUE") == 0) {
	    /* checkbox is checked */
	    gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(secure_tf_checkbox),
					 TRUE );    
	} else {
	    /* checkbox is unchecked */
	    gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(secure_tf_checkbox),
					 FALSE );    
	}

	gtk_table_attach_defaults(GTK_TABLE(table), secure_tf_checkbox, 0, 2, 5, 6);
	
	gtk_widget_show (secure_tf_checkbox);


	/* Descriptive label above the human-readable date dialogue box */

	label = gtk_label_new("\nCookie expiration dates are stored as\nthe number of seconds since 1 Jan 1970.\nAs you edit the human-readble date below,\nstick to the following format:\nWdy Mon dd hh:mm:ss yyyy and\ngtkcookie will be able to figure\nout the time in seconds for you.\n");
 
	gtk_widget_show (label);
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 2, 6, 7);

/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */
	/* Expiry Date (in seconds) Label */
	label = gtk_label_new(glbl_cookie_titles_str[COL_EXPIRE_SECONDS]);
	exp_s_label = gtk_label_new(glbl_selected_cookie[COL_EXPIRE_SECONDS]);
	/* have our global GtkWidget pointer point to the expiry date
	   label, so that we can change the label even after this function
	   has run */
	glbl_selected_etl = exp_s_label;
 
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 7, 8);
	gtk_table_attach_defaults(GTK_TABLE(table), exp_s_label, 1, 2, 7, 8);
	gtk_widget_show (label);
	gtk_widget_show (exp_s_label);


/* {COL_DOMAIN, COL_SUFFIX, COL_PATH, COL_SECURE, COL_EXPIRE_SECONDS, COL_NAME, COL_VALUE, COL_EXPIRE_DATE}; */
	/* COL_EXPIRE_DATE Edit Box */
	label = gtk_label_new(glbl_cookie_titles_str[COL_EXPIRE_DATE]);
 
	text_entry[COL_EXPIRE_DATE] = gtk_entry_new ();
 
	/* have to find a way to pass a pointer to the expiry date in seconds
	   label to this function, so that the function can change the label.
	   In the function itself, use the Linux-specific call strptime()
	   to take care of parsing the date as it is edited. */
	gtk_signal_connect(GTK_OBJECT(text_entry[COL_EXPIRE_DATE]), "changed",
			   GTK_SIGNAL_FUNC(change_time_callback),
			   glbl_int_ptr[COL_EXPIRE_DATE]);
	gtk_entry_set_text (GTK_ENTRY (text_entry[COL_EXPIRE_DATE]),
			    glbl_selected_cookie[COL_EXPIRE_DATE]);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 8, 9);
	gtk_table_attach_defaults(GTK_TABLE(table), text_entry[COL_EXPIRE_DATE], 1, 2, 8, 9);
	gtk_widget_show (label);
	gtk_widget_show (text_entry[COL_EXPIRE_DATE]);

	gtk_widget_show (table);

	/* if we're done with the cookie here, nuke it and
	   see what happens */
	/* free_cookie(glbl_selected_cookie); */
	/* OK, what happens is that the cookie really does contain
	   pointers right into the glbl_clist's elements. Freeing the memory
	   that the cookie's gchar pointers point to fucks up the
	   elements in the glbl_clist */

	/* To the best of my knowlege, gtk_entry_set_text copies
	   the string from the pointer into the text widget, but
	   de-allocation of the text seems to be the responsibility
	   of the widget */
	
	separator = gtk_hseparator_new ();
	gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
	gtk_widget_show (separator);

/* buttons at bottom */

	box2 = gtk_vbox_new (FALSE, 10);
	gtk_container_border_width (GTK_CONTAINER (box2), 10);
	gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
	gtk_widget_show (box2);


	
	hbox = gtk_hbox_new (TRUE, 10);
	

	OK_button = gtk_button_new_with_label ("OK");
	/* note that I pass a pointer to this window as the second arg of
	   this function, because update_glbl_clist() destroys
	   the widget in arg2. This is useful, because in this case,
	   I want the edit window to be destroyed after the OK
	   button is clicked. */
	gtk_signal_connect (GTK_OBJECT (OK_button), "clicked",
			    GTK_SIGNAL_FUNC(update_glbl_clist),
			    GTK_OBJECT(window));
	gtk_box_pack_start (GTK_BOX (hbox), OK_button, TRUE, TRUE, 0);
	GTK_WIDGET_SET_FLAGS (OK_button, GTK_CAN_DEFAULT);
	gtk_widget_grab_default (OK_button);
	gtk_widget_show (OK_button);
	gtk_widget_show (hbox);


	Cancel_button = gtk_button_new_with_label ("Cancel");
	gtk_signal_connect_object (GTK_OBJECT (Cancel_button), "clicked",
				   GTK_SIGNAL_FUNC(gtk_widget_destroy),
				   GTK_OBJECT (window));
	gtk_box_pack_start (GTK_BOX (hbox), Cancel_button, TRUE, TRUE, 0);
	GTK_WIDGET_SET_FLAGS (Cancel_button, GTK_CAN_DEFAULT);
	gtk_widget_show (Cancel_button);
	gtk_widget_show (hbox);

	gtk_box_pack_start (GTK_BOX (box2), hbox, TRUE, TRUE, 0);
    }

    if (!GTK_WIDGET_VISIBLE (window)) {
	gtk_widget_show (window);
    } else {
	gtk_widget_destroy (window);
    }
}


static void change_callback(GtkWidget *entry, gpointer func_data) {
    gchar *entry_text;
    int i;  /* the number of the cookie column */
    size_t field_length;

    i = *(int *) func_data;
    entry_text = gtk_entry_get_text(GTK_ENTRY(entry));

    if (entry_text != NULL) {
	field_length = strlen(entry_text);
	/* shouldn't I free the memory before re-allocating it? YES! */
	if (glbl_selected_cookie[i] != NULL) {
	    free(glbl_selected_cookie[i]);
	}
	glbl_selected_cookie[i] = calloc(field_length + 1, sizeof(gchar));
	strncpy(glbl_selected_cookie[i], entry_text, field_length);
	glbl_selected_cookie[i][field_length] = '\0';
    }
}


static void change_time_callback(GtkWidget *entry, gpointer func_data) {
    gchar *entry_text;
    int i;  /* the number of the cookie column */
    size_t field_length;

    i = *(int *) func_data;
    entry_text = gtk_entry_get_text(GTK_ENTRY(entry));

    if (entry_text != NULL) {
	field_length = strlen(entry_text);
	/* shouldn't I free the memory before re-allocating it? YES! */
	if (glbl_selected_cookie[i] != NULL) {
	    free(glbl_selected_cookie[i]);
	}
	glbl_selected_cookie[i] = calloc(field_length + 1, sizeof(gchar));
	strncpy(glbl_selected_cookie[i], entry_text, field_length);
	glbl_selected_cookie[i][field_length] = '\0';

	strptime(glbl_selected_cookie[i], "%a %b %d %T %Y", &glbl_selected_time);
	glbl_selected_time_sec = mktime(&glbl_selected_time);
	sprintf(glbl_selected_time_str, "%i", (int)glbl_selected_time_sec);
	gtk_label_set( GTK_LABEL(glbl_selected_etl), glbl_selected_time_str);
	/* gtk_label_set( GTK_LABEL(glbl_selected_etl), glbl_selected_cookie[4]); */

	/* remember, below, that glbl_selected_cookie[4] is the date in seconds */
	if (glbl_selected_time_str != NULL) {
	    field_length = strlen(glbl_selected_time_str);
	    /* shouldn't I free the memory before re-allocating it? YES! */
	    if (glbl_selected_cookie[4] != NULL) {
		free(glbl_selected_cookie[4]);
	    }
	    glbl_selected_cookie[4] = calloc(field_length + 1, sizeof(gchar));
	    strncpy(glbl_selected_cookie[4], glbl_selected_time_str, field_length);
	    glbl_selected_cookie[4][field_length] = '\0';
	}
    }
}

static void change_tf_callback(GtkWidget *toggle_button, gpointer func_data) {
    int i;  /* the number of the cookie column */
    size_t field_length;
    gchar *True = "TRUE";
    gchar *False = "FALSE";
    gchar *Bool = NULL;
    
    i = *(int *) func_data;

    if (GTK_TOGGLE_BUTTON (toggle_button)->active) {
	Bool = True;
    } else {
	Bool = False;
    }
    field_length = strlen(Bool);
    /* shouldn't I free the memory before re-allocating it? YES! */
    if (glbl_selected_cookie[i] != NULL) {
	free(glbl_selected_cookie[i]);
    }
    glbl_selected_cookie[i] = calloc(field_length + 1, sizeof(gchar));
    strncpy(glbl_selected_cookie[i], Bool, field_length);
    glbl_selected_cookie[i][field_length] = '\0';
}

/* static void update_clist(GtkWidget *clist, gpointer window) { */
static void update_glbl_clist(GtkWidget *calling_widget,
			      GtkWidget *edit_win_to_destroy) {
    int column;
    for (column = 0; column < COOKIE_COLUMNS; column++) {
	gtk_clist_set_text(GTK_CLIST(glbl_clist), glbl_clist_selected_row, column,
                               glbl_selected_cookie[column] );
    }
    glbl_last_sorted_column = -1;  /* because we just set the text
				      in a column somewhere
				      in the clist, no columns
				      can be assumed to be
				      sorted anymore */

    gtk_widget_destroy(edit_win_to_destroy);
}


/* FILE SELECTION STUFF */


static void file_selection_ok (GtkWidget *w, GtkFileSelection *fs) {
    size_t length;
    gchar *filename;
    filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
    if (glbl_selected_filename_str != NULL) {
	free(glbl_selected_filename_str);
    }
    if (filename != NULL) {
	length = strlen(filename);
	glbl_selected_filename_str = calloc(length + 1, sizeof(gchar));
	strncpy(glbl_selected_filename_str, filename, length);
	glbl_selected_filename_str[length] = '\0';
    }
    gtk_widget_destroy (GTK_WIDGET (fs));
    /* clear the glbl_clist */
    clear_glbl_clist();
    populate_glbl_clist();
}

static void show_open_file_dialog_box (GtkWidget *calling_widget, gpointer func_data) {
    static GtkWidget *window = NULL;

    if (!window) {
	window = gtk_file_selection_new ("Open");

	gtk_file_selection_hide_fileop_buttons (GTK_FILE_SELECTION (window));

	gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);

	gtk_signal_connect (GTK_OBJECT (window), "destroy",
			    GTK_SIGNAL_FUNC(gtk_widget_destroyed),
			    &window);

	gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (window)->ok_button),
			    "clicked", GTK_SIGNAL_FUNC(file_selection_ok),
			    window);
	gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (window)->cancel_button),
				   "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
				   GTK_OBJECT (window));
      
    }
  
    if (!GTK_WIDGET_VISIBLE (window)) {
	gtk_widget_show (window);
    } else {
	gtk_widget_destroy (window);
    }
}


static void show_save_as_dialog_box (GtkWidget *calling_widget, gpointer func_data) {
    static GtkWidget *window = NULL;

    if (!window) {
	window = gtk_file_selection_new ("Save As");

	gtk_file_selection_hide_fileop_buttons (GTK_FILE_SELECTION (window));

	gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);

	gtk_signal_connect (GTK_OBJECT (window), "destroy",
			    GTK_SIGNAL_FUNC(gtk_widget_destroyed),
			    &window);

	gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (window)->ok_button),
			    "clicked", GTK_SIGNAL_FUNC(save_as_ok),
			    window);
	gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (window)->cancel_button),
				   "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
				   GTK_OBJECT (window));

	if (glbl_selected_filename_str != NULL) {
	    gtk_file_selection_set_filename ( GTK_FILE_SELECTION(window), glbl_selected_filename_str);
	}
    }
  
    if (!GTK_WIDGET_VISIBLE (window)) {
	gtk_widget_show (window);
    } else {
	gtk_widget_destroy (window);
    }
}


static void save_as_ok (GtkWidget *w, GtkFileSelection *fs) {
    size_t length;
    gchar *filename;
    filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
    if (glbl_selected_filename_str != NULL) {
	free(glbl_selected_filename_str);  /* don't I set to null as well? 55555 */
    }
    if (filename != NULL) {
	length = strlen(filename);
	glbl_selected_filename_str = calloc(length + 1, sizeof(gchar));
	strncpy(glbl_selected_filename_str, filename, length);
	glbl_selected_filename_str[length] = '\0';
    }
    gtk_widget_destroy (GTK_WIDGET (fs));

    /* set the title of the main window to show the selected filename */
    /* first, erase what's currently in the gstring */
    g_string_erase(glbl_window_title_gstr, 0, glbl_window_title_gstr->len);
    /* now create the new title, always starting with "gtkcookie - " and
       then the filename */
    g_string_assign(glbl_window_title_gstr, "gtkcookie - ");
    g_string_append(glbl_window_title_gstr, glbl_selected_filename_str);
    /* set the title of the main window */
    gtk_window_set_title(GTK_WINDOW (glbl_main_window), glbl_window_title_gstr->str);

    /* now that glbl_selected_filename_str has got the new value,
       call save_file(), which will save glbl_selected_filename_str */
    /* note too that save_file is a callback function, which means
       that it can be passed a pointer to the calling widget and to
       extra function data, neither of which we need here; hence,
       the two NULLs */
    save_file(NULL, NULL);
}

static void check_for_running_netscape(void) {
    uid_t uid_of_this_process;
    gboolean error;  /* was there an error trying to find
			a process named "netscape"? */
    pid_t netscape_pid = 0;
    GString *error_msg_gstr;

    uid_of_this_process = getuid();
    /* if a "netscape" is running under the same uid of this
       programme, then we know the user is running netscape */
    netscape_pid = process_is_running("netscape", uid_of_this_process, &error);
    /* note that Red Had actually runs netscape as "netscape-commun",
       so check for that too, if the first check revealed nothing */
    if (netscape_pid == 0) {
	netscape_pid = process_is_running("netscape", uid_of_this_process, &error);
    }
	
    if (netscape_pid != 0) {
	error_msg_gstr = g_string_new("");
	g_string_sprintf(error_msg_gstr, "It looks like you're running Netscape at pid %i right now.\nPlease note that your changes to your netscape cookie file\nwill be lost if you leave Netscape running.\nNetscape writes the cookie file just before exiting,\nand could therefore clobber any changes you make to the file now.", netscape_pid);
	show_error_dialog_box(error_msg_gstr->str);
	/* don't forget to deallocate the string after we're done
	   with it! Fortunately, deallocating the string seems not
	   to harm the error message created by show_error_dialog_box() */
	g_string_free(error_msg_gstr, TRUE);
    }
}

static void
search (GtkWidget* wdg, gpointer data) {
    gint row;
    gint column;
	gchar *needle, *cell_contents;

    /* time to try to use gstrings, methinks... */
    /* oooh, gstrings are sexy! */
    GString *cell_str;  /* the string from a cell in the clist */
    GString *find_str;  /* the string in the "find" text entry box */

	needle = (gchar*) gtk_entry_get_text (GTK_ENTRY (data));
	if (!strlen (needle)) {
		return;
	}
	find_str = g_string_new (needle);

    /* if the search is case-insensitive, make everything in
       find_str uppercase */
    if (glbl_search_case_sensitive == FALSE) {
	g_string_up(find_str);
    }
    
    for (row = glbl_clist_selected_row; row < glbl_clist_rows; row++) {
	for (column = 0; column < COOKIE_COLUMNS; column++) {
	    gtk_clist_get_text(GTK_CLIST (glbl_clist), row, column,
			       &cell_contents);
	    
	    /* we need to copy construct cell_str, so that when we
	       uppercase cell_str, we are not uppercasing the actual
	       clist cell data that cell_str points to! */
	    cell_str = g_string_new("");
	    g_string_append(cell_str, cell_contents);

	    /* if the search is case-insensitive, make everything in
	       cell_str uppercase */
	    if (glbl_search_case_sensitive == FALSE) {
		g_string_up(cell_str);
	    }
	    
	    if (strstr(cell_str->str, find_str->str) != NULL
		&& row > glbl_last_found_row) {
		/* in the if statement directly above, we don't count
		   a search that is successful in the last found row,
		   because that search has already been done, and the
		   user is interested in rows AFTER the current row.
		   In the case of a match after the last found row,
		   set the last found row to the currently matched row
		   so that the number is accurate for the next time
		   this function is called */
		glbl_last_found_row = row;
		gtk_clist_unselect_row(GTK_CLIST(glbl_clist), row, column);
		/* manni: perhaps if you don't check for visibility, but
		   simply always do the moveto, you will conquer the
		   bug where a row is sometimes just off the display
		   area (due to the bottom scrollbar?) */
		if (gtk_clist_row_is_visible(GTK_CLIST(glbl_clist), row) ==
					     GTK_VISIBILITY_NONE) {
		    gtk_clist_moveto(GTK_CLIST(glbl_clist), row, column,
				     0.5, 0.5);
		}
		gtk_clist_select_row(GTK_CLIST(glbl_clist), row, column);
		select_clist(glbl_clist, row, column, NULL);
		/* DEALLOCATE! */
		g_string_free(cell_str, TRUE);
		g_string_free(find_str, TRUE);
		return;
	    }
	    /* DEALLOCATE! */
	    g_string_free(cell_str, TRUE);
	}
    }
    /* DEALLOCATE! */
    g_string_free(find_str, TRUE);
    
    /* if we made it this far, we must have a not found condition! */
    show_error_dialog_box("Not found.");
}

static void
clear_search (GtkWidget* wdg, gpointer data) {
	gtk_entry_set_text (GTK_ENTRY (data), "");
    /* the last found row is rendered invalid when the user clears
       the text box, so reset that variable to -1 */
    glbl_last_found_row = -1;
}

static void reset_last_found_row(GtkWidget *calling_widget, gpointer func_data) {
    glbl_last_found_row = -1;
}

/* seems not to be working
static void set_cursor (guint c) {
    GdkCursor *cursor;

    c = CLAMP (c, 0, 152);
    c &= 0xfe;

    cursor = gdk_cursor_new (c);
    gdk_window_set_cursor (glbl_main_window->window, cursor);
    gdk_cursor_destroy (cursor);
}
*/

static void quicksort_clist(GtkWidget *clist,
			    SORT_STYLE sort_style,
			    gint sort_column,
			    gint first_index,
			    gint last_index) {

    /* clist is the clist */
    /* sort column is the column that we are sorting on */
    /* first_index is the index of the first element of the (sub)array */
    /* n is the number of elements in the clist */
    gint pivot_index;  /* index of the pivot element */

    if (first_index < last_index) {
	/* partition the array and set the pivot_index */
	partition_clist(clist, sort_style, sort_column, first_index, &pivot_index, last_index);

	/* recursive calls will now sort the subarrays */
	quicksort_clist(clist, sort_style, sort_column, first_index, (pivot_index - 1));
	quicksort_clist(clist, sort_style, sort_column, (pivot_index + 1), last_index);
    }
}

static void partition_clist(GtkWidget *clist,
			    SORT_STYLE sort_style,
			    gint sort_column,
			    gint first_index,
			    gint *pivot_index,
			    gint last_index) {

    gchar *pivot_cookie[COOKIE_COLUMNS];
    gchar *sort_column_cell_contents;  /* text inside the cell to be sorted */
    gchar *cell_contents;  /* generic contents of any cell */
    gchar *more_cell_contents;  /* generic contents of any cell */
    gint col;  /* column */
    int (*compfunction)(const char*, const char*); /* pointer to
						      a comparison function
						      that takes two strings
						      as its args */

    gint too_big_index = first_index + 1;
    gint too_small_index = last_index;  /* hopefully this makes
					   the code more readable,
					   not less -- besides which,
					last_index isn't allowed to
					change, whereas too_small_index
					will be changing a lot */

    initialise_cookie(pivot_cookie);  /* always do this with a new cookie! */

    /* choose the first cookie as the pivot_cookie */
    copy_clist_row_to_cookie(first_index, pivot_cookie);

    /* make the comparison function either strcmp from the
     standard C library, or intcmp, defined by me in this code. */
    if (sort_style == STRING) {
	compfunction = strcmp;
    } else {
	compfunction = intcmp;
    }
    while (too_big_index <= too_small_index) {
	gtk_clist_get_text(GTK_CLIST (clist), too_big_index, sort_column,
			   &sort_column_cell_contents);
	/* with strcmp, instead use a pointer to some function
	   depending on the sort column, that way we can have the
	   function return true or false depending on whatever comparison
	   is necessary for the sort column. This way, we can do
	   a strcmp for strings, but a atol on both strings, and
	   a numeric comparison for dates */
	/* SORT: strcmp <= 0 */
	/* while (too_big_index <= last_index && strcmp(sort_column_cell_contents, pivot_cookie[sort_column]) <= 0) { */
	while (too_big_index <= last_index && (*compfunction)(sort_column_cell_contents, pivot_cookie[sort_column]) <= 0) {
	    ++too_big_index;
	    gtk_clist_get_text(GTK_CLIST (clist), too_big_index, sort_column,
			       &sort_column_cell_contents);
	}
	gtk_clist_get_text(GTK_CLIST (clist), too_small_index, sort_column,
			   &sort_column_cell_contents);
	/* SORT: strcmp > 0 */
	/* while (strcmp(sort_column_cell_contents, pivot_cookie[sort_column]) > 0) { */
	while ((*compfunction)(sort_column_cell_contents, pivot_cookie[sort_column]) > 0) {
	    --too_small_index;
	    gtk_clist_get_text(GTK_CLIST (clist), too_small_index, sort_column,
			       &sort_column_cell_contents);
	}
	if (too_big_index < too_small_index) {
	    swap_row_clist(clist, too_big_index, too_small_index);
	    
	    gtk_clist_get_text(GTK_CLIST (clist), too_big_index, sort_column,
			       &cell_contents);
	    gtk_clist_get_text(GTK_CLIST (clist), too_small_index, sort_column,
			       &more_cell_contents);
	}
    }

    *pivot_index = too_small_index;
    /* move the data at the pivot_index (which still contains the value that is
       less than or equal to the pivot) to the first slot. Unless, of course,
       first_index and *pivot_index are already pointing to the same slot. x*/
    if (first_index != *pivot_index) {
	for (col = 0; col < COOKIE_COLUMNS; col++) {
	    gtk_clist_get_text(GTK_CLIST (clist), *pivot_index, col,
			       &cell_contents);
	    gtk_clist_set_text(GTK_CLIST (clist), first_index, col,
			       cell_contents);
	}
    }
    /* move what's in the pivot_cookie to the *pivot_index in the clist */
    for (col = 0; col < COOKIE_COLUMNS; col++) {
	gtk_clist_set_text(GTK_CLIST (clist), *pivot_index, col,
			   pivot_cookie[col]);
    }
    
    /* DEALLOCATE pivot_cookie */
    free_cookie(pivot_cookie);
}


/* set case sensitive searching TRUE/FALSE */
/* (held in the global variable glbl_search_case_sensitive) */
static void set_tf_case_sensitive_search(GtkWidget *toggle_button, gpointer func_data) {
    if (GTK_TOGGLE_BUTTON (toggle_button)->active) {
	glbl_search_case_sensitive = TRUE;
    } else {
	glbl_search_case_sensitive = FALSE;
    }
}

static gint
search_dlg_key_cb (GtkWidget* wdg, GdkEventKey* ev, gpointer data) {
	if (ev->keyval == GDK_Return) {
		search (wdg, data);
		return TRUE;
	} else if (ev->keyval == GDK_Escape) {
		gtk_widget_destroy (wdg);
	return TRUE;  /* tell calling code that we have handled this event */
    }
    return FALSE;
}


gint handle_keypress_in_edit_win(GtkWidget *edit_window, GdkEventKey *event, gpointer func_data) {
    if (event->keyval == GDK_Return) {
	/* note how the first arg below is NULL. update_glbl_clist
	   often gets called as a callback function, so a pointer to
	   the calling widget is generally passed as the first argument.
	   But here, I'm calling it manually, so I just leave the first
	   arg NULL. Doesn't seem to break anything, either. */
	update_glbl_clist(NULL, edit_window);
	return TRUE;  /* tell calling code that we have handled this event */
    }
    if (event->keyval == GDK_Escape) {
	gtk_widget_destroy(edit_window);
	return TRUE;  /* tell calling code that we have handled this event */
    }
    return FALSE;
}


gint handle_keypress_in_main_win(GtkWidget *calling_widget, GdkEventKey *event, gpointer func_data) {
    if (event->keyval == GDK_Delete) {
	/* note arg1 below is null, because with callback functions,
	   arg1 is automatically populated with a pointer to the
	   calling widget;
	   arg2 is null because it's a pointer to any function data
	   you may need to pass to the function... which we don't */
	remove_selected_row_from_glbl_clist(NULL, NULL);
	return TRUE;  /* tell calling code that we have handled this event */
    }
    return FALSE;
}

/* end */