aboutsummaryrefslogtreecommitdiffstats
path: root/e-util
diff options
context:
space:
mode:
authorChris Toshok <toshok@ximian.com>2002-11-15 10:47:18 +0800
committerChris Toshok <toshok@src.gnome.org>2002-11-15 10:47:18 +0800
commitb39be5377514b9bc3c43b7f5172645edd683e683 (patch)
tree0d55ddbc296c5451fb0af8ca4ffd5295963375a3 /e-util
parentb9b2089527b62590f70625208d7d68579a51124d (diff)
downloadgsoc2013-evolution-b39be5377514b9bc3c43b7f5172645edd683e683.tar.gz
gsoc2013-evolution-b39be5377514b9bc3c43b7f5172645edd683e683.tar.zst
gsoc2013-evolution-b39be5377514b9bc3c43b7f5172645edd683e683.zip
deal with EBitArray/ESorter being a GObject now.
2002-11-14 Chris Toshok <toshok@ximian.com> * gal/widgets/e-selection-model-array.c: deal with EBitArray/ESorter being a GObject now. * gal/widgets/e-selection-model-simple.c: same. * gal/util/e-bit-array.[ch]: this derives from GObject now. * gal/util/e-sorter.[ch]: same. * gal/util/e-sorter-array.[ch]: same. svn path=/trunk/; revision=18774
Diffstat (limited to 'e-util')
-rw-r--r--e-util/e-bit-array.c27
-rw-r--r--e-util/e-bit-array.h16
-rw-r--r--e-util/e-sorter-array.c39
-rw-r--r--e-util/e-sorter-array.h14
-rw-r--r--e-util/e-sorter.c31
-rw-r--r--e-util/e-sorter.h17
6 files changed, 53 insertions, 91 deletions
diff --git a/e-util/e-bit-array.c b/e-util/e-bit-array.c
index 66a7c38af2..2a79e1bab2 100644
--- a/e-util/e-bit-array.c
+++ b/e-util/e-bit-array.c
@@ -26,9 +26,7 @@
#include "e-bit-array.h"
#include "gal/util/e-util.h"
-#define EBA_CLASS(e) ((EBitArrayClass *)((GtkObject *)e)->klass)
-
-#define PARENT_TYPE (gtk_object_get_type ())
+#define PARENT_TYPE G_TYPE_OBJECT
#define ONES ((guint32) 0xffffffff)
@@ -38,7 +36,7 @@
#define BITMASK_LEFT(n) ((((n) % 32) == 0) ? 0 : (ONES << (32 - ((n) % 32))))
#define BITMASK_RIGHT(n) ((guint32)(((guint32) ONES) >> ((n) % 32)))
-static GtkObjectClass *parent_class;
+static GObjectClass *parent_class;
static void
e_bit_array_insert_real(EBitArray *eba, int row)
@@ -141,17 +139,18 @@ e_bit_array_move_row(EBitArray *eba, int old_row, int new_row)
}
static void
-eba_destroy (GtkObject *object)
+eba_dispose (GObject *object)
{
EBitArray *eba;
eba = E_BIT_ARRAY (object);
- g_free(eba->data);
+ if (eba->data)
+ g_free(eba->data);
eba->data = NULL;
- if (GTK_OBJECT_CLASS (parent_class)->destroy)
- (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
+ if (G_OBJECT_CLASS (parent_class)->dispose)
+ (* G_OBJECT_CLASS (parent_class)->dispose) (object);
}
/**
@@ -408,13 +407,13 @@ e_bit_array_init (EBitArray *eba)
static void
e_bit_array_class_init (EBitArrayClass *klass)
{
- GtkObjectClass *object_class;
+ GObjectClass *object_class;
- parent_class = gtk_type_class (PARENT_TYPE);
+ parent_class = g_type_class_ref (PARENT_TYPE);
- object_class = GTK_OBJECT_CLASS(klass);
+ object_class = G_OBJECT_CLASS(klass);
- object_class->destroy = eba_destroy;
+ object_class->dispose = eba_dispose;
}
E_MAKE_TYPE(e_bit_array, "EBitArray", EBitArray,
@@ -423,10 +422,8 @@ E_MAKE_TYPE(e_bit_array, "EBitArray", EBitArray,
EBitArray *
e_bit_array_new (int count)
{
- EBitArray *eba = gtk_type_new (e_bit_array_get_type ());
+ EBitArray *eba = g_object_new (E_BIT_ARRAY_TYPE, NULL);
eba->bit_count = count;
eba->data = g_new0(guint32, (eba->bit_count + 31) / 32);
- gtk_object_ref (GTK_OBJECT (eba));
- gtk_object_sink (GTK_OBJECT (eba));
return eba;
}
diff --git a/e-util/e-bit-array.h b/e-util/e-bit-array.h
index c06f405ede..ebfe644da6 100644
--- a/e-util/e-bit-array.h
+++ b/e-util/e-bit-array.h
@@ -24,17 +24,17 @@
#ifndef _E_BIT_ARRAY_H_
#define _E_BIT_ARRAY_H_
-#include <gtk/gtkobject.h>
+#include <glib-object.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define E_BIT_ARRAY_TYPE (e_bit_array_get_type ())
-#define E_BIT_ARRAY(o) (GTK_CHECK_CAST ((o), E_BIT_ARRAY_TYPE, EBitArray))
-#define E_BIT_ARRAY_CLASS(k) (GTK_CHECK_CLASS_CAST((k), E_BIT_ARRAY_TYPE, EBitArrayClass))
-#define E_IS_BIT_ARRAY(o) (GTK_CHECK_TYPE ((o), E_BIT_ARRAY_TYPE))
-#define E_IS_BIT_ARRAY_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_BIT_ARRAY_TYPE))
+#define E_BIT_ARRAY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), E_BIT_ARRAY_TYPE, EBitArray))
+#define E_BIT_ARRAY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), E_BIT_ARRAY_TYPE, EBitArrayClass))
+#define E_IS_BIT_ARRAY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), E_BIT_ARRAY_TYPE))
+#define E_IS_BIT_ARRAY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), E_BIT_ARRAY_TYPE))
#ifndef _E_FOREACH_FUNC_H_
#define _E_FOREACH_FUNC_H_
@@ -43,18 +43,18 @@ typedef void (*EForeachFunc) (int model_row,
#endif
typedef struct {
- GtkObject base;
+ GObject base;
gint bit_count;
guint32 *data;
} EBitArray;
typedef struct {
- GtkObjectClass parent_class;
+ GObjectClass parent_class;
} EBitArrayClass;
-GtkType e_bit_array_get_type (void);
+GType e_bit_array_get_type (void);
EBitArray *e_bit_array_new (int count);
gboolean e_bit_array_value_at (EBitArray *selection,
diff --git a/e-util/e-sorter-array.c b/e-util/e-sorter-array.c
index ffc4d355df..ab1e748d9a 100644
--- a/e-util/e-sorter-array.c
+++ b/e-util/e-sorter-array.c
@@ -24,18 +24,12 @@
#include <config.h>
#include <stdlib.h>
#include <string.h>
-#include <gtk/gtksignal.h>
#include "gal/util/e-util.h"
#include "e-sorter-array.h"
#define d(x)
-/* The arguments we take */
-enum {
- ARG_0
-};
-
-#define PARENT_TYPE e_sorter_get_type()
+#define PARENT_TYPE E_SORTER_TYPE
#define INCREMENT_AMOUNT 100
@@ -233,44 +227,17 @@ e_sorter_array_construct (ESorterArray *esa,
ESorterArray *
e_sorter_array_new (ECompareRowsFunc compare, gpointer closure)
{
- ESorterArray *esa = gtk_type_new (E_SORTER_ARRAY_TYPE);
+ ESorterArray *esa = g_object_new (E_SORTER_ARRAY_TYPE, NULL);
return e_sorter_array_construct (esa, compare, closure);
}
static void
-esa_destroy (GtkObject *object)
-{
- GTK_OBJECT_CLASS (parent_class)->destroy (object);
-}
-
-static void
-esa_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
-{
- switch (arg_id) {
- default:
- break;
- }
-}
-
-static void
-esa_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
-{
- switch (arg_id) {
- }
-}
-
-static void
esa_class_init (ESorterArrayClass *klass)
{
- GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass);
ESorterClass *sorter_class = E_SORTER_CLASS(klass);
- parent_class = gtk_type_class (PARENT_TYPE);
-
- object_class->destroy = esa_destroy;
- object_class->set_arg = esa_set_arg;
- object_class->get_arg = esa_get_arg;
+ parent_class = g_type_class_ref (PARENT_TYPE);
sorter_class->model_to_sorted = esa_model_to_sorted ;
sorter_class->sorted_to_model = esa_sorted_to_model ;
diff --git a/e-util/e-sorter-array.h b/e-util/e-sorter-array.h
index 803b67052a..227e437443 100644
--- a/e-util/e-sorter-array.h
+++ b/e-util/e-sorter-array.h
@@ -24,17 +24,17 @@
#ifndef _E_SORTER_ARRAY_H_
#define _E_SORTER_ARRAY_H_
-#include <gtk/gtkobject.h>
-#include <gal/util/e-sorter.h>
#include <glib.h>
+#include <glib-object.h>
+#include <gal/util/e-sorter.h>
G_BEGIN_DECLS
#define E_SORTER_ARRAY_TYPE (e_sorter_array_get_type ())
-#define E_SORTER_ARRAY(o) (GTK_CHECK_CAST ((o), E_SORTER_ARRAY_TYPE, ESorterArray))
-#define E_SORTER_ARRAY_CLASS(k) (GTK_CHECK_CLASS_CAST((k), E_SORTER_ARRAY_TYPE, ESorterArrayClass))
-#define E_IS_SORTER_ARRAY(o) (GTK_CHECK_TYPE ((o), E_SORTER_ARRAY_TYPE))
-#define E_IS_SORTER_ARRAY_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_SORTER_ARRAY_TYPE))
+#define E_SORTER_ARRAY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), E_SORTER_ARRAY_TYPE, ESorterArray))
+#define E_SORTER_ARRAY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), E_SORTER_ARRAY_TYPE, ESorterArrayClass))
+#define E_IS_SORTER_ARRAY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), E_SORTER_ARRAY_TYPE))
+#define E_IS_SORTER_ARRAY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), E_SORTER_ARRAY_TYPE))
#ifndef _E_COMPARE_ROWS_FUNC_H_
#define _E_COMPARE_ROWS_FUNC_H_
@@ -60,7 +60,7 @@ typedef struct {
ESorterClass parent_class;
} ESorterArrayClass;
-GtkType e_sorter_array_get_type (void);
+GType e_sorter_array_get_type (void);
ESorterArray *e_sorter_array_construct (ESorterArray *sorter,
ECompareRowsFunc compare,
gpointer closure);
diff --git a/e-util/e-sorter.c b/e-util/e-sorter.c
index 2040241abe..adee6d0d98 100644
--- a/e-util/e-sorter.c
+++ b/e-util/e-sorter.c
@@ -23,18 +23,15 @@
#include <config.h>
#include <stdlib.h>
-#include <gtk/gtksignal.h>
#include <string.h>
#include "gal/util/e-util.h"
#include "e-sorter.h"
#define d(x)
-#define PARENT_TYPE gtk_object_get_type()
+#define PARENT_TYPE G_TYPE_OBJECT
-static GtkObjectClass *parent_class;
-
-#define ES_CLASS(es) ((ESorterClass *)((GTypeInstance *)(es))->g_class)
+static GObjectClass *parent_class;
static gint es_model_to_sorted (ESorter *es, int row);
static gint es_sorted_to_model (ESorter *es, int row);
@@ -45,7 +42,7 @@ static gboolean es_needs_sorting(ESorter *es);
static void
es_class_init (ESorterClass *klass)
{
- parent_class = gtk_type_class (PARENT_TYPE);
+ parent_class = g_type_class_ref (PARENT_TYPE);
klass->model_to_sorted = es_model_to_sorted;
klass->sorted_to_model = es_sorted_to_model;
@@ -64,7 +61,7 @@ E_MAKE_TYPE(e_sorter, "ESorter", ESorter, es_class_init, es_init, PARENT_TYPE)
ESorter *
e_sorter_new (void)
{
- ESorter *es = gtk_type_new (E_SORTER_TYPE);
+ ESorter *es = g_object_new (E_SORTER_TYPE, NULL);
return es;
}
@@ -106,8 +103,8 @@ e_sorter_model_to_sorted (ESorter *es, int row)
g_return_val_if_fail(es != NULL, -1);
g_return_val_if_fail(row >= 0, -1);
- if (ES_CLASS(es)->model_to_sorted)
- return ES_CLASS(es)->model_to_sorted (es, row);
+ if (E_SORTER_GET_CLASS(es)->model_to_sorted)
+ return E_SORTER_GET_CLASS(es)->model_to_sorted (es, row);
else
return -1;
}
@@ -118,8 +115,8 @@ e_sorter_sorted_to_model (ESorter *es, int row)
g_return_val_if_fail(es != NULL, -1);
g_return_val_if_fail(row >= 0, -1);
- if (ES_CLASS(es)->sorted_to_model)
- return ES_CLASS(es)->sorted_to_model (es, row);
+ if (E_SORTER_GET_CLASS(es)->sorted_to_model)
+ return E_SORTER_GET_CLASS(es)->sorted_to_model (es, row);
else
return -1;
}
@@ -130,8 +127,8 @@ e_sorter_get_model_to_sorted_array (ESorter *es, int **array, int *count)
{
g_return_if_fail(es != NULL);
- if (ES_CLASS(es)->get_model_to_sorted_array)
- ES_CLASS(es)->get_model_to_sorted_array (es, array, count);
+ if (E_SORTER_GET_CLASS(es)->get_model_to_sorted_array)
+ E_SORTER_GET_CLASS(es)->get_model_to_sorted_array (es, array, count);
}
void
@@ -139,8 +136,8 @@ e_sorter_get_sorted_to_model_array (ESorter *es, int **array, int *count)
{
g_return_if_fail(es != NULL);
- if (ES_CLASS(es)->get_sorted_to_model_array)
- ES_CLASS(es)->get_sorted_to_model_array (es, array, count);
+ if (E_SORTER_GET_CLASS(es)->get_sorted_to_model_array)
+ E_SORTER_GET_CLASS(es)->get_sorted_to_model_array (es, array, count);
}
@@ -149,8 +146,8 @@ e_sorter_needs_sorting(ESorter *es)
{
g_return_val_if_fail (es != NULL, FALSE);
- if (ES_CLASS(es)->needs_sorting)
- return ES_CLASS(es)->needs_sorting (es);
+ if (E_SORTER_GET_CLASS(es)->needs_sorting)
+ return E_SORTER_GET_CLASS(es)->needs_sorting (es);
else
return FALSE;
}
diff --git a/e-util/e-sorter.h b/e-util/e-sorter.h
index 5a89fa4c59..a70c2bd2d7 100644
--- a/e-util/e-sorter.h
+++ b/e-util/e-sorter.h
@@ -24,24 +24,25 @@
#ifndef _E_SORTER_H_
#define _E_SORTER_H_
-#include <gtk/gtkobject.h>
+#include <glib-object.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define E_SORTER_TYPE (e_sorter_get_type ())
-#define E_SORTER(o) (GTK_CHECK_CAST ((o), E_SORTER_TYPE, ESorter))
-#define E_SORTER_CLASS(k) (GTK_CHECK_CLASS_CAST((k), E_SORTER_TYPE, ESorterClass))
-#define E_IS_SORTER(o) (GTK_CHECK_TYPE ((o), E_SORTER_TYPE))
-#define E_IS_SORTER_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_SORTER_TYPE))
+#define E_SORTER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), E_SORTER_TYPE, ESorter))
+#define E_SORTER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), E_SORTER_TYPE, ESorterClass))
+#define E_IS_SORTER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), E_SORTER_TYPE))
+#define E_IS_SORTER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), E_SORTER_TYPE))
+#define E_SORTER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), E_SORTER_TYPE, ESorterClass))
typedef struct {
- GtkObject base;
+ GObject base;
} ESorter;
typedef struct {
- GtkObjectClass parent_class;
+ GObjectClass parent_class;
gint (*model_to_sorted) (ESorter *sorter,
int row);
gint (*sorted_to_model) (ESorter *sorter,
@@ -57,7 +58,7 @@ typedef struct {
gboolean (*needs_sorting) (ESorter *sorter);
} ESorterClass;
-GtkType e_sorter_get_type (void);
+GType e_sorter_get_type (void);
ESorter *e_sorter_new (void);
gint e_sorter_model_to_sorted (ESorter *sorter,
1' href='#n851'>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 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Michael Zucchi <notzed@ximian.com>
 *
 *  Copyright 2003 Ximian, Inc. (www.ximian.com)
 *
 *  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 Street #330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <gtk/gtkvbox.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtkvpaned.h>
#include <gdk/gdkkeysyms.h>

#include <libgnome/gnome-url.h>

#include <libgnomeprintui/gnome-print-dialog.h>

#include <gconf/gconf-client.h>

#include <gal/menus/gal-view-etable.h>
#include <gal/menus/gal-view-factory-etable.h>
#include <gal/menus/gal-view-instance.h>
#include "widgets/menus/gal-view-menus.h"

#include <camel/camel-mime-message.h>
#include <camel/camel-stream.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-mime-filter.h>
#include <camel/camel-mime-filter-tohtml.h>
#include <camel/camel-mime-filter-enriched.h>
#include <camel/camel-multipart.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-url.h>
#include <camel/camel-vee-folder.h>

#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-generic-factory.h>
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-ui-component.h>
#include <bonobo/bonobo-ui-util.h>

#include "widgets/misc/e-charset-picker.h"
#include "e-util/e-error.h"

#include <e-util/e-dialog-utils.h>
#include <e-util/e-icon-factory.h>
#include <e-util/e-print.h>
#include <e-util/e-profile-event.h>

#include "filter/filter-rule.h"

#include "em-format-html-display.h"
#include "em-format-html-print.h"
#include "em-folder-selection.h"
#include "em-folder-view.h"
#include "em-mailer-prefs.h"
#include "em-message-browser.h"
#include "message-list.h"
#include "em-utils.h"
#include "em-composer-utils.h"
#include "em-marshal.h"
#include "em-menu.h"
#include "em-event.h"

#include <gtkhtml/gtkhtml.h>

#include "mail-mt.h"
#include "mail-ops.h"
#include "mail-config.h"
#include "mail-autofilter.h"
#include "mail-vfolder.h"
#include "mail-component.h"
#include "mail-tools.h"

#include "evolution-shell-component-utils.h" /* Pixmap stuff, sigh */

static void emfv_list_message_selected(MessageList *ml, const char *uid, EMFolderView *emfv);
static void emfv_list_built(MessageList *ml, EMFolderView *emfv);
static int emfv_list_right_click(ETree *tree, gint row, ETreePath path, gint col, GdkEvent *event, EMFolderView *emfv);
static void emfv_list_double_click(ETree *tree, gint row, ETreePath path, gint col, GdkEvent *event, EMFolderView *emfv);
static int emfv_list_key_press(ETree *tree, int row, ETreePath path, int col, GdkEvent *ev, EMFolderView *emfv);
static void emfv_list_selection_change(ETree *tree, EMFolderView *emfv);

static void emfv_format_link_clicked(EMFormatHTMLDisplay *efhd, const char *uri, EMFolderView *);
static int emfv_format_popup_event(EMFormatHTMLDisplay *efhd, GdkEventButton *event, const char *uri, CamelMimePart *part, EMFolderView *);

static void emfv_enable_menus(EMFolderView *emfv);

static void emfv_set_folder(EMFolderView *emfv, CamelFolder *folder, const char *uri);
static void emfv_set_folder_uri(EMFolderView *emfv, const char *uri);
static void emfv_set_message(EMFolderView *emfv, const char *uid, int nomarkseen);
static void emfv_activate(EMFolderView *emfv, BonoboUIComponent *uic, int state);

static void emfv_message_reply(EMFolderView *emfv, int mode);
static void vfolder_type_current (EMFolderView *emfv, int type);
static void filter_type_current (EMFolderView *emfv, int type);

static void emfv_setting_setup(EMFolderView *emfv);

static void emfv_on_url_cb(GObject *emitter, const char *url, EMFolderView *emfv);
static void emfv_on_url(EMFolderView *emfv, const char *uri, const char *nice_uri);

static void emfv_set_seen (EMFolderView *emfv, const char *uid);

static gboolean emfv_popup_menu (GtkWidget *widget);

static const EMFolderViewEnable emfv_enable_map[];

struct _EMFolderViewPrivate {
    guint seen_id;
    guint setting_notify_id;
    guint selected_id;
    int nomarkseen:1;
    int destroyed:1;

    GtkWidget *invisible;
    char *selection_uri;

    GalViewInstance *view_instance;
    GalViewMenus *view_menus;

    char *selected_uid;
};

static GtkVBoxClass *emfv_parent;

enum {
    EMFV_ON_URL,
    EMFV_LOADED,
    EMFV_CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static void emfv_selection_get(GtkWidget *widget, GtkSelectionData *data, guint info, guint time_stamp, EMFolderView *emfv);
static void emfv_selection_clear_event(GtkWidget *widget, GdkEventSelection *event, EMFolderView *emfv);

#ifdef ENABLE_PROFILING
static void
emfv_format_complete(EMFormat *emf, EMFolderView *emfv)
{
    e_profile_event_emit("goto.done", emf->uid?emf->uid:"", 0);
}
#endif

static void
emfv_init(GObject *o)
{
    EMFolderView *emfv = (EMFolderView *)o;
    struct _EMFolderViewPrivate *p;
    extern CamelSession *session;
    
    gtk_box_set_homogeneous (GTK_BOX (emfv), FALSE);

    p = emfv->priv = g_malloc0(sizeof(struct _EMFolderViewPrivate));

    emfv->statusbar_active = TRUE;
    emfv->list_active = FALSE;
    
    emfv->ui_files = g_slist_append(NULL, EVOLUTION_UIDIR "/evolution-mail-message.xml");
    emfv->ui_app_name = "evolution-mail";

    emfv->enable_map = g_slist_prepend(NULL, (void *)emfv_enable_map);

    emfv->list = (MessageList *)message_list_new();
    g_signal_connect(emfv->list, "message_selected", G_CALLBACK(emfv_list_message_selected), emfv);
    g_signal_connect(emfv->list, "message_list_built", G_CALLBACK(emfv_list_built), emfv);

    /* FIXME: should this hang off message-list instead? */
    g_signal_connect(emfv->list->tree, "right_click", G_CALLBACK(emfv_list_right_click), emfv);
    g_signal_connect(emfv->list->tree, "double_click", G_CALLBACK(emfv_list_double_click), emfv);
    g_signal_connect(emfv->list->tree, "key_press", G_CALLBACK(emfv_list_key_press), emfv);
    g_signal_connect(emfv->list->tree, "selection_change", G_CALLBACK(emfv_list_selection_change), emfv);

    emfv->preview = (EMFormatHTMLDisplay *)em_format_html_display_new();
    /* FIXME: set_session should NOT be called here.  Should it be a constructor attribute? */
    em_format_set_session ((EMFormat *) emfv->preview, session);
    g_signal_connect(emfv->preview, "link_clicked", G_CALLBACK(emfv_format_link_clicked), emfv);
    g_signal_connect(emfv->preview, "popup_event", G_CALLBACK(emfv_format_popup_event), emfv);
    g_signal_connect (emfv->preview, "on_url", G_CALLBACK (emfv_on_url_cb), emfv);
#ifdef ENABLE_PROFILING
    g_signal_connect(emfv->preview, "complete", G_CALLBACK (emfv_format_complete), emfv);
#endif
    p->invisible = gtk_invisible_new();
    g_signal_connect(p->invisible, "selection_get", G_CALLBACK(emfv_selection_get), emfv);
    g_signal_connect(p->invisible, "selection_clear_event", G_CALLBACK(emfv_selection_clear_event), emfv);
    gtk_selection_add_target(p->invisible, GDK_SELECTION_PRIMARY, GDK_SELECTION_TYPE_STRING, 0);
    gtk_selection_add_target(p->invisible, GDK_SELECTION_CLIPBOARD, GDK_SELECTION_TYPE_STRING, 1);

    emfv->async = mail_async_event_new();

    emfv_setting_setup(emfv);
}

static void
emfv_finalise(GObject *o)
{
    EMFolderView *emfv = (EMFolderView *)o;
    struct _EMFolderViewPrivate *p = emfv->priv;

    g_slist_free(emfv->ui_files);
    g_slist_free(emfv->enable_map);

    g_free(p);

    ((GObjectClass *)emfv_parent)->finalize(o);
}

static void
emfv_destroy (GtkObject *o)
{
    EMFolderView *emfv = (EMFolderView *) o;
    struct _EMFolderViewPrivate *p = emfv->priv;

    p->destroyed = TRUE;

    if (p->seen_id) {
        g_source_remove(p->seen_id);
        p->seen_id = 0;
    }

    if (p->setting_notify_id) {
        GConfClient *gconf = gconf_client_get_default();

        gconf_client_notify_remove(gconf, p->setting_notify_id);
        p->setting_notify_id = 0;
        g_object_unref(gconf);
    }

    if (emfv->folder) {
        camel_object_unref(emfv->folder);
        g_free(emfv->folder_uri);
        emfv->folder = NULL;
        emfv->folder_uri = NULL;
    }

    if (emfv->async) {
        mail_async_event_destroy(emfv->async);
        emfv->async = NULL;
    }

    if (p->invisible) {
        gtk_object_destroy((GtkObject *)p->invisible);
        p->invisible = NULL;
    }

    if (p->selected_id != 0) {
        g_source_remove(p->selected_id);
        p->selected_id = 0;
    }

    g_free(p->selected_uid);
    p->selected_uid = NULL;

    emfv->preview = NULL;
    emfv->list = NULL;
    emfv->preview_active = FALSE;
    emfv->uic = NULL;

    ((GtkObjectClass *) emfv_parent)->destroy (o);
}

static void
emfv_class_init(GObjectClass *klass)
{
    klass->finalize = emfv_finalise;
    
    ((GtkObjectClass *) klass)->destroy = emfv_destroy;
    
    ((GtkWidgetClass *) klass)->popup_menu = emfv_popup_menu;
    
    ((EMFolderViewClass *) klass)->update_message_style = TRUE;
    
    ((EMFolderViewClass *)klass)->set_folder = emfv_set_folder;
    ((EMFolderViewClass *)klass)->set_folder_uri = emfv_set_folder_uri;
    ((EMFolderViewClass *)klass)->set_message = emfv_set_message;
    ((EMFolderViewClass *)klass)->activate = emfv_activate;

    ((EMFolderViewClass *)klass)->on_url = emfv_on_url;
    
    signals[EMFV_ON_URL] = g_signal_new ("on-url",
                         G_OBJECT_CLASS_TYPE (klass),
                         G_SIGNAL_RUN_LAST,
                         G_STRUCT_OFFSET (EMFolderViewClass, on_url),
                         NULL, NULL,
                         em_marshal_VOID__STRING_STRING,
                         G_TYPE_NONE,
                         2, G_TYPE_STRING, G_TYPE_STRING);

    signals[EMFV_LOADED] = g_signal_new("loaded",
                        G_OBJECT_CLASS_TYPE(klass),
                        G_SIGNAL_RUN_LAST,
                        G_STRUCT_OFFSET(EMFolderViewClass, loaded),
                        NULL, NULL,
                        g_cclosure_marshal_VOID__VOID,
                        G_TYPE_NONE,
                        0);

    signals[EMFV_CHANGED] = g_signal_new("changed",
                         G_OBJECT_CLASS_TYPE(klass),
                         G_SIGNAL_RUN_LAST,
                         G_STRUCT_OFFSET(EMFolderViewClass, changed),
                         NULL, NULL,
                         g_cclosure_marshal_VOID__VOID,
                         G_TYPE_NONE,
                         0);
}

GType
em_folder_view_get_type(void)
{
    static GType type = 0;

    if (type == 0) {
        static const GTypeInfo info = {
            sizeof(EMFolderViewClass),
            NULL, NULL,
            (GClassInitFunc)emfv_class_init,
            NULL, NULL,
            sizeof(EMFolderView), 0,
            (GInstanceInitFunc)emfv_init
        };
        emfv_parent = g_type_class_ref(gtk_vbox_get_type());
        type = g_type_register_static(gtk_vbox_get_type(), "EMFolderView", &info, 0);
    }

    return type;
}

GtkWidget *em_folder_view_new(void)
{
    EMFolderView *emfv = g_object_new(em_folder_view_get_type(), 0);

    return (GtkWidget *)emfv;
}

/* flag all selected messages. Return number flagged */
/* FIXME: Should this be part of message-list instead? */
int
em_folder_view_mark_selected(EMFolderView *emfv, guint32 mask, guint32 set)
{
    GPtrArray *uids;
    int i;

    if (emfv->folder == NULL)
        return 0;
    
    uids = message_list_get_selected(emfv->list);
    camel_folder_freeze(emfv->folder);

    for (i=0; i<uids->len; i++)
        camel_folder_set_message_flags(emfv->folder, uids->pdata[i], mask, set);

    message_list_free_uids(emfv->list, uids);
    camel_folder_thaw(emfv->folder);
    
    return i;
}

/* should this be elsewhere/take a uid list? */
int
em_folder_view_open_selected(EMFolderView *emfv)
{
    GPtrArray *uids, *views;
    int i = 0;
    
    uids = message_list_get_selected(emfv->list);

    if (uids->len >= 10) {
        char *num = g_strdup_printf("%d", uids->len);
        int doit;

        doit = em_utils_prompt_user((GtkWindow *)gtk_widget_get_toplevel((GtkWidget *)emfv),
                        "/apps/evolution/mail/prompts/open_many",
                        "mail:ask-open-many", num, NULL);
        g_free(num);
        if (!doit) {
            message_list_free_uids(emfv->list, uids);
            return 0;
        }
    }

        if (em_utils_folder_is_drafts(emfv->folder, emfv->folder_uri)
        || em_utils_folder_is_outbox(emfv->folder, emfv->folder_uri)) {
        em_utils_edit_messages(emfv->folder, uids, TRUE);
        return uids->len;
    }

    /* for vfolders we need to edit the *original*, not the vfolder copy */
    views = g_ptr_array_new();
    for (i=0;i<uids->len;i++) {
        if (camel_object_is((CamelObject *)emfv->folder, camel_vee_folder_get_type())) {
            CamelVeeMessageInfo *vinfo = (CamelVeeMessageInfo *)camel_folder_get_message_info(emfv->folder, uids->pdata[i]);

            if (vinfo) {
                char *uid;
                /* TODO: get_location shouldn't strdup the uid */
                CamelFolder *f = camel_vee_folder_get_location((CamelVeeFolder *)emfv->folder, vinfo, &uid);
                char *uri = mail_tools_folder_to_url(f);

                if (em_utils_folder_is_drafts(f, uri) || em_utils_folder_is_outbox(f, uri)) {
                    GPtrArray *edits = g_ptr_array_new();

                    g_ptr_array_add(edits, uid);
                    em_utils_edit_messages(f, edits, TRUE);
                } else {
                    g_free(uid);
                    g_ptr_array_add(views, g_strdup(uids->pdata[i]));
                }
                g_free(uri);
            }
        } else {
            g_ptr_array_add(views, g_strdup(uids->pdata[i]));
        }
    }

    /* TODO: have an em_utils_open_messages call? */
    for (i=0; i<views->len; i++) {
        EMMessageBrowser *emmb;

        emmb = (EMMessageBrowser *)em_message_browser_window_new();
        message_list_set_threaded(((EMFolderView *)emmb)->list, emfv->list->threaded);
        message_list_set_search(((EMFolderView *)emmb)->list, emfv->list->search);
        em_folder_view_set_hide_deleted((EMFolderView *)emmb, emfv->hide_deleted);
        /* FIXME: session needs to be passed easier than this */
        em_format_set_session((EMFormat *)((EMFolderView *)emmb)->preview, ((EMFormat *)emfv->preview)->session);
        em_folder_view_set_folder((EMFolderView *)emmb, emfv->folder, emfv->folder_uri);
        em_folder_view_set_message((EMFolderView *)emmb, views->pdata[i], FALSE);
        gtk_widget_show(emmb->window);
        /* TODO: this loads the message twice (!) */
        em_utils_handle_receipt (emfv->folder, uids->pdata[i], NULL);
        g_free(views->pdata[i]);
    }
    g_ptr_array_free(views, TRUE);

    message_list_free_uids(emfv->list, uids);

    return i;
}

/* ******************************************************************************** */
static void
emfv_list_display_view(GalViewInstance *instance, GalView *view, EMFolderView *emfv)
{
    if (GAL_IS_VIEW_ETABLE(view))
        gal_view_etable_attach_tree(GAL_VIEW_ETABLE(view), emfv->list->tree);
}

static void
emfv_setup_view_instance(EMFolderView *emfv)
{
    struct _EMFolderViewPrivate *p = emfv->priv;
    gboolean outgoing;
    char *id;
    static GalViewCollection *collection = NULL;

    g_assert(emfv->folder);
    g_assert(emfv->folder_uri);

    if (collection == NULL) {
        ETableSpecification *spec;
        GalViewFactory *factory;
        const char *evolution_dir;
        char *dir;

        collection = gal_view_collection_new ();
    
        gal_view_collection_set_title (collection, _("Mail"));
    
        evolution_dir = mail_component_peek_base_directory (mail_component_peek ());
        dir = g_build_filename (evolution_dir, "mail", "views", NULL);
        gal_view_collection_set_storage_directories (collection, EVOLUTION_GALVIEWSDIR "/mail/", dir);
        g_free (dir);
    
        spec = e_table_specification_new ();
        e_table_specification_load_from_file (spec, EVOLUTION_ETSPECDIR "/message-list.etspec");
    
        factory = gal_view_factory_etable_new (spec);
        g_object_unref (spec);
        gal_view_collection_add_factory (collection, factory);
        g_object_unref (factory);
    
        gal_view_collection_load (collection);
    }

    if (p->view_instance) {
        g_object_unref(p->view_instance);
        p->view_instance = NULL;
    }

    if (p->view_menus) {
        g_object_unref(p->view_menus);
        p->view_menus = NULL;
    }

    outgoing = em_utils_folder_is_drafts (emfv->folder, emfv->folder_uri)
        || em_utils_folder_is_sent (emfv->folder, emfv->folder_uri)
        || em_utils_folder_is_outbox (emfv->folder, emfv->folder_uri);
    
    /* TODO: should this go through mail-config api? */
    id = mail_config_folder_to_safe_url (emfv->folder);
    p->view_instance = gal_view_instance_new (collection, id);
    g_free (id);
    
    if (outgoing)
        gal_view_instance_set_default_view(p->view_instance, "As_Sent_Folder");
    
    gal_view_instance_load(p->view_instance);
    
    if (!gal_view_instance_exists(p->view_instance)) {
        struct stat st;
        char *path;
        
        path = mail_config_folder_to_cachename (emfv->folder, "et-header-");
        if (path && stat (path, &st) == 0 && st.st_size > 0 && S_ISREG (st.st_mode)) {
            ETableSpecification *spec;
            ETableState *state;
            GalView *view;
            
            spec = e_table_specification_new ();
            e_table_specification_load_from_file (spec, EVOLUTION_ETSPECDIR "/message-list.etspec");
            view = gal_view_etable_new (spec, "");
            g_object_unref (spec);
            
            state = e_table_state_new ();
            e_table_state_load_from_file (state, path);
            gal_view_etable_set_state (GAL_VIEW_ETABLE (view), state);
            g_object_unref (state);
            
            gal_view_instance_set_custom_view(p->view_instance, view);
            g_object_unref (view);
        }
        
        g_free (path);
    }

    g_signal_connect(p->view_instance, "display_view", G_CALLBACK(emfv_list_display_view), emfv);
    emfv_list_display_view(p->view_instance, gal_view_instance_get_current_view(p->view_instance), emfv);
    
    if (emfv->list_active && emfv->uic) {
        p->view_menus = gal_view_menus_new(p->view_instance);
        gal_view_menus_apply(p->view_menus, emfv->uic, NULL);
    }
}

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

static void
emfv_set_folder(EMFolderView *emfv, CamelFolder *folder, const char *uri)
{
    int isout = (folder && uri
             && (em_utils_folder_is_drafts(folder, uri)
             || em_utils_folder_is_sent(folder, uri)
             || em_utils_folder_is_outbox(folder, uri)));
    
    if (folder == emfv->folder)
        return;

    if (emfv->priv->selected_id)
        g_source_remove(emfv->priv->selected_id);
    
    if (emfv->preview)
        em_format_format ((EMFormat *) emfv->preview, NULL, NULL, NULL);
    
    message_list_set_folder(emfv->list, folder, uri, isout);
    g_free(emfv->folder_uri);
    emfv->folder_uri = g_strdup(uri);
    
    if (emfv->folder) {
        emfv->hide_deleted = emfv->list->hidedeleted; /* <- a bit nasty but makes it track the display better */
        mail_sync_folder (emfv->folder, NULL, NULL);
        camel_object_unref(emfv->folder);
    }

    emfv->folder = folder;
    if (folder) {
        /* We need to set this up to get the right view options for the message-list, 
         * even if we're not showing it */
        emfv_setup_view_instance(emfv);
    }
    
    emfv_enable_menus(emfv);

    /* TODO: should probably be called after all processing, not just this class's impl */
    g_signal_emit(emfv, signals[EMFV_LOADED], 0);
}

static void
emfv_got_folder(char *uri, CamelFolder *folder, void *data)
{
    EMFolderView *emfv = data;
    
    em_folder_view_set_folder(emfv, folder, uri);
}

static void
emfv_set_folder_uri(EMFolderView *emfv, const char *uri)
{
    mail_get_folder(uri, 0, emfv_got_folder, emfv, mail_thread_queued);
}

static void
emfv_set_message(EMFolderView *emfv, const char *uid, int nomarkseen)
{
    e_profile_event_emit("goto.uid", uid?uid:"<none>", 0);

    /* This could possible race with other set messages, but likelyhood is small */
    emfv->priv->nomarkseen = nomarkseen;
    message_list_select_uid(emfv->list, uid);
    /* force an update, since we may not get an updated event if we select the same uid */
    emfv_list_message_selected(emfv->list, uid, emfv);
}

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

static void
emfv_selection_get(GtkWidget *widget, GtkSelectionData *data, guint info, guint time_stamp, EMFolderView *emfv)
{
    struct _EMFolderViewPrivate *p = emfv->priv;

    if (p->selection_uri == NULL)
        return;

    gtk_selection_data_set(data, data->target, 8, p->selection_uri, strlen(p->selection_uri));
}

static void
emfv_selection_clear_event(GtkWidget *widget, GdkEventSelection *event, EMFolderView *emfv)
{
#if 0 /* do i care? */
    struct _EMFolderViewPrivate *p = emfv->priv;

    g_free(p->selection_uri);
    p->selection_uri = NULL;
#endif
}

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

/* Popup menu
   In many cases these are the functions called by the bonobo callbacks too */

static void
emfv_popup_open(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    em_folder_view_open_selected(emfv);
}

static void
emfv_popup_edit (EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids;

    if (!em_utils_check_user_can_send_mail((GtkWidget *)emfv))
        return;
    
    uids = message_list_get_selected(emfv->list);
    em_utils_edit_messages (emfv->folder, uids, FALSE);
}

static void
emfv_popup_saveas(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids;
    
    uids = message_list_get_selected(emfv->list);
    em_utils_save_messages((GtkWidget *)emfv, emfv->folder, uids);
}

static void
emfv_view_load_images(BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;

    if (emfv->preview)
        em_format_html_load_http((EMFormatHTML *)emfv->preview);
}

static void
emfv_popup_load_images(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;  
    emfv_view_load_images(NULL, data, NULL);
}

static void
emfv_popup_print(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    em_folder_view_print(emfv, FALSE);
}

static void
emfv_popup_copy_text(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    gtk_html_copy (((EMFormatHTML *)emfv->preview)->html);
}

static void
emfv_popup_source(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    EMMessageBrowser *emmb;
    GPtrArray *uids;
    
    uids = message_list_get_selected(emfv->list);

    emmb = (EMMessageBrowser *)em_message_browser_window_new();
    em_format_set_session((EMFormat *)((EMFolderView *)emmb)->preview, ((EMFormat *)emfv->preview)->session);
    em_folder_view_set_folder((EMFolderView *)emmb, emfv->folder, emfv->folder_uri);
    em_format_set_mode((EMFormat *)((EMFolderView *)emmb)->preview, EM_FORMAT_SOURCE);
    em_folder_view_set_message((EMFolderView *)emmb, uids->pdata[0], FALSE);
    gtk_widget_show(emmb->window);

    message_list_free_uids(emfv->list, uids);
}

static void
emfv_mail_compose(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    if (!em_utils_check_user_can_send_mail((GtkWidget *)emfv))
        return;

    em_utils_compose_new_message(emfv->folder_uri);
}

static void
emfv_popup_selectall(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    gtk_html_select_all (((EMFormatHTML *)emfv->preview)->html);

}

static void
emfv_popup_reply_sender(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    emfv_message_reply(emfv, REPLY_MODE_SENDER);
}

static void
emfv_popup_reply_list(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    emfv_message_reply(emfv, REPLY_MODE_LIST);
}

static void
emfv_popup_reply_all(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    emfv_message_reply(emfv, REPLY_MODE_ALL);
}

static void
emfv_popup_forward(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids;

    if (!em_utils_check_user_can_send_mail((GtkWidget *)emfv))
        return;

    uids = message_list_get_selected(emfv->list);
    em_utils_forward_messages (emfv->folder, uids, emfv->folder_uri);
}

static void
emfv_popup_flag_followup(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids = message_list_get_selected(emfv->list);

    em_utils_flag_for_followup((GtkWidget *)emfv, emfv->folder, uids);
}

static void
emfv_popup_flag_completed(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids;
    
    uids = message_list_get_selected(emfv->list);
    em_utils_flag_for_followup_completed((GtkWidget *)emfv, emfv->folder, uids);
}

static void
emfv_popup_flag_clear(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids = message_list_get_selected(emfv->list);

    em_utils_flag_for_followup_clear((GtkWidget *)emfv, emfv->folder, uids);
}

static void
emfv_popup_mark_read(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    em_folder_view_mark_selected(emfv, CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);
}

static void
emfv_popup_mark_unread(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    em_folder_view_mark_selected(emfv, CAMEL_MESSAGE_SEEN|CAMEL_MESSAGE_DELETED, 0);
    
    if (emfv->priv->seen_id) {
        g_source_remove(emfv->priv->seen_id);
        emfv->priv->seen_id = 0;
    }
}

static void
emfv_popup_mark_important(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    em_folder_view_mark_selected(emfv, CAMEL_MESSAGE_FLAGGED|CAMEL_MESSAGE_DELETED, CAMEL_MESSAGE_FLAGGED);
}

static void
emfv_popup_mark_unimportant(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    em_folder_view_mark_selected(emfv, CAMEL_MESSAGE_FLAGGED, 0);
}

static void
emfv_popup_mark_junk (EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    int count;
    
    count = em_folder_view_mark_selected(emfv,
                         CAMEL_MESSAGE_SEEN|CAMEL_MESSAGE_JUNK|CAMEL_MESSAGE_JUNK_LEARN,
                         CAMEL_MESSAGE_SEEN|CAMEL_MESSAGE_JUNK|CAMEL_MESSAGE_JUNK_LEARN);
    if (count == 1)
        message_list_select(emfv->list, MESSAGE_LIST_SELECT_NEXT, 0, 0);
}

static void
emfv_popup_mark_nojunk (EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    int count;
    
    count = em_folder_view_mark_selected(emfv,
                         CAMEL_MESSAGE_JUNK|CAMEL_MESSAGE_JUNK_LEARN,
                         CAMEL_MESSAGE_JUNK_LEARN);
    if (count == 1)
        message_list_select(emfv->list, MESSAGE_LIST_SELECT_NEXT, 0, 0);
}

static void
emfv_popup_delete(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    int count;
    
    count = em_folder_view_mark_selected(emfv, CAMEL_MESSAGE_SEEN|CAMEL_MESSAGE_DELETED, CAMEL_MESSAGE_SEEN|CAMEL_MESSAGE_DELETED);
    
    if (count == 1) {
        if (!message_list_select (emfv->list, MESSAGE_LIST_SELECT_NEXT, 0, 0) && emfv->hide_deleted)
            message_list_select (emfv->list, MESSAGE_LIST_SELECT_PREVIOUS, 0, 0);
    }
}

static void
emfv_popup_undelete(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    em_folder_view_mark_selected(emfv, CAMEL_MESSAGE_DELETED, 0);
}

struct _move_data {
    EMFolderView *emfv;
    GPtrArray *uids;
    int delete;
};

static char *default_xfer_messages_uri = NULL;

static void
emfv_popup_move_cb(const char *uri, void *data)
{
    struct _move_data *d = data;
    
    if (uri) {
        g_free (default_xfer_messages_uri);
        default_xfer_messages_uri = g_strdup (uri);
        mail_transfer_messages(d->emfv->folder, d->uids, d->delete, uri, 0, NULL, NULL);
    } else
        em_utils_uids_free(d->uids);
    
    g_object_unref(d->emfv);
    g_free(d);
}

static void
emfv_popup_move(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    struct _move_data *d;
    
    d = g_malloc(sizeof(*d));
    d->emfv = emfv;
    g_object_ref(emfv);
    d->uids = message_list_get_selected(emfv->list);
    d->delete = TRUE;
    
    em_select_folder ((GtkWindow *) emfv, _("Select folder"), _("_Move"), default_xfer_messages_uri, NULL, emfv_popup_move_cb, d);
}

static void
emfv_popup_copy(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    struct _move_data *d;
    
    d = g_malloc(sizeof(*d));
    d->emfv = emfv;
    g_object_ref(emfv);
    d->uids = message_list_get_selected(emfv->list);
    d->delete = FALSE;
    
    em_select_folder ((GtkWindow *) emfv, _("Select folder"), _("C_opy"), default_xfer_messages_uri, NULL, emfv_popup_move_cb, d);
}

static void
emfv_set_label(EMFolderView *emfv, const char *label)
{
    GPtrArray *uids = message_list_get_selected(emfv->list);
    int i;

    for (i=0;i<uids->len;i++)
        camel_folder_set_message_user_tag(emfv->folder, uids->pdata[i], "label", label);

    message_list_free_uids(emfv->list, uids);
}

static void
emfv_popup_label_clear(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    emfv_set_label(emfv, NULL);
}

static void
emfv_popup_label_set(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;

    emfv_set_label(emfv, pitem->user_data);
}

static void
emfv_popup_add_sender(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids = message_list_get_selected(emfv->list);
    CamelMessageInfo *info;
    const char *addr;

    if (uids->len == 1
        && (info = camel_folder_get_message_info(emfv->folder, uids->pdata[0])) != NULL
        && (addr = camel_message_info_from(info)) != NULL
        && addr[0] != 0)
        em_utils_add_address((GtkWidget *)emfv, addr);

    em_utils_uids_free(uids);
}

static void
emfv_popup_apply_filters(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids = message_list_get_selected(emfv->list);

    mail_filter_on_demand(emfv->folder, uids);
}

static void
emfv_popup_filter_junk(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    GPtrArray *uids = message_list_get_selected(emfv->list);

    mail_filter_junk(emfv->folder, uids);
}

/* filter callbacks, this will eventually be a wizard, see
   filter_type_current/vfolder_type_current for implementation */

#define EMFV_POPUP_AUTO_TYPE(autotype, name, type)  \
static void                     \
name(EPopup *ep, EPopupItem *item, void *data)      \
{                           \
    EMFolderView *emfv = data;          \
    autotype(emfv, type);               \
}

EMFV_POPUP_AUTO_TYPE(vfolder_type_current, emfv_popup_vfolder_subject, AUTO_SUBJECT)
EMFV_POPUP_AUTO_TYPE(vfolder_type_current, emfv_popup_vfolder_sender, AUTO_FROM)
EMFV_POPUP_AUTO_TYPE(vfolder_type_current, emfv_popup_vfolder_recipients, AUTO_TO)
EMFV_POPUP_AUTO_TYPE(vfolder_type_current, emfv_popup_vfolder_mlist, AUTO_MLIST)

EMFV_POPUP_AUTO_TYPE(filter_type_current, emfv_popup_filter_subject, AUTO_SUBJECT)
EMFV_POPUP_AUTO_TYPE(filter_type_current, emfv_popup_filter_sender, AUTO_FROM)
EMFV_POPUP_AUTO_TYPE(filter_type_current, emfv_popup_filter_recipients, AUTO_TO)
EMFV_POPUP_AUTO_TYPE(filter_type_current, emfv_popup_filter_mlist, AUTO_MLIST)

/* TODO: Move some of these to be 'standard' menu's */

static EPopupItem emfv_popup_items[] = {
    { E_POPUP_ITEM, "00.emfv.00", N_("_Copy"), emfv_popup_copy_text, NULL, "stock_copy", EM_FOLDER_VIEW_SELECT_DISPLAY|EM_FOLDER_VIEW_SELECT_SELECTION },

    { E_POPUP_BAR, "10.emfv", NULL, NULL, NULL, NULL },

    {E_POPUP_ITEM, "10.emfv.00", N_("Reply to _All"), emfv_popup_reply_all, NULL, "stock_mail-reply-to-all", EM_POPUP_SELECT_ONE },
    { E_POPUP_ITEM, "10.emfv.01", N_("_Reply to Sender"), emfv_popup_reply_sender, NULL, "stock_mail-reply", EM_POPUP_SELECT_ONE },
    { E_POPUP_ITEM, "10.emfv.02", N_("_Forward"), emfv_popup_forward, NULL, "stock_mail-forward", EM_POPUP_SELECT_MANY },

    { E_POPUP_BAR, "20.emfv", NULL, NULL, NULL, NULL },
    { E_POPUP_ITEM, "20.emfv.00", N_("_Edit as New Message..."), emfv_popup_edit, NULL, NULL, EM_POPUP_SELECT_EDIT },
    { E_POPUP_ITEM, "20.emfv.01", N_("_Save As..."), emfv_popup_saveas, NULL, "stock_save-as", EM_POPUP_SELECT_MANY },
    { E_POPUP_ITEM, "20.emfv.02", N_("_Print..."), emfv_popup_print, NULL, "stock_print", EM_POPUP_SELECT_ONE },

    { E_POPUP_BAR, "40.emfv", NULL, NULL, NULL, NULL },
    { E_POPUP_ITEM, "40.emfv.00", N_("_Delete"), emfv_popup_delete, NULL, "stock_delete", EM_POPUP_SELECT_DELETE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "40.emfv.01", N_("U_ndelete"), emfv_popup_undelete, NULL, "stock_undelete", EM_POPUP_SELECT_UNDELETE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "40.emfv.02", N_("Mo_ve to Folder..."), emfv_popup_move, NULL, "stock_mail-move", EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "40.emfv.03", N_("_Copy to Folder..."), emfv_popup_copy, NULL, "stock_mail-copy", EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_LISTONLY },

    { E_POPUP_BAR, "50.emfv", NULL, NULL, NULL, NULL },
    { E_POPUP_ITEM, "50.emfv.00", N_("Mar_k as Read"), emfv_popup_mark_read, NULL, "stock_mail-open", EM_POPUP_SELECT_MARK_READ|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "50.emfv.01", N_("Mark as U_nread"), emfv_popup_mark_unread, NULL, "stock_mail-unread", EM_POPUP_SELECT_MARK_UNREAD|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "50.emfv.02", N_("Mark as _Important"), emfv_popup_mark_important, NULL, "stock_mail-priority-high", EM_POPUP_SELECT_MARK_IMPORTANT|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "50.emfv.03", N_("_Mark as Unimportant"), emfv_popup_mark_unimportant, NULL, NULL, EM_POPUP_SELECT_MARK_UNIMPORTANT|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "50.emfv.04", N_("Mark as _Junk"), emfv_popup_mark_junk, NULL, "stock_spam", EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "50.emfv.05", N_("Mark as _Not Junk"), emfv_popup_mark_nojunk, NULL, "stock_not-spam", EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "50.emfv.06", N_("Mark for Follo_w Up..."), emfv_popup_flag_followup, NULL, "stock_mail-flag-for-followup",  EM_POPUP_SELECT_FLAG_FOLLOWUP|EM_FOLDER_VIEW_SELECT_LISTONLY },

    { E_POPUP_SUBMENU, "60.label.00", N_("Label"), NULL, NULL, NULL, EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "60.label.00/00.label", N_("None"), emfv_popup_label_clear, NULL, NULL, EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_BAR, "60.label.00/00.label.00", NULL, NULL, NULL, NULL },

    { E_POPUP_BAR, "70.emfv.06", NULL, NULL, NULL, NULL },

    { E_POPUP_ITEM, "70.emfv.07", N_("Fla_g Completed"), emfv_popup_flag_completed, NULL, "stock_mail-flag-for-followup-done", EM_POPUP_SELECT_FLAG_COMPLETED|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "70.emfv.08", N_("Cl_ear Flag"), emfv_popup_flag_clear, NULL, NULL, EM_POPUP_SELECT_FLAG_CLEAR|EM_FOLDER_VIEW_SELECT_LISTONLY },
 
    { E_POPUP_BAR, "90.filter", NULL, NULL, NULL, NULL },
    { E_POPUP_SUBMENU, "90.filter.00", N_("Crea_te Rule From Message"), NULL, NULL, NULL, EM_POPUP_SELECT_ONE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "90.filter.00/00.00", N_("vFolder on _Subject"), emfv_popup_vfolder_subject, NULL, NULL, EM_POPUP_SELECT_ONE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "90.filter.00/00.01", N_("vFolder on Se_nder"), emfv_popup_vfolder_sender, NULL, NULL, EM_POPUP_SELECT_ONE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "90.filter.00/00.02", N_("vFolder on _Recipients"), emfv_popup_vfolder_recipients, NULL, NULL, EM_POPUP_SELECT_ONE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "90.filter.00/00.03", N_("vFolder on Mailing _List"),
      emfv_popup_vfolder_mlist, NULL, NULL, EM_POPUP_SELECT_ONE|EM_POPUP_SELECT_MAILING_LIST|EM_FOLDER_VIEW_SELECT_LISTONLY },
    
    { E_POPUP_BAR, "90.filter.00/10", NULL, NULL, NULL, NULL },
    { E_POPUP_ITEM, "90.filter.00/10.00", N_("Filter on Sub_ject"), emfv_popup_filter_subject, NULL, NULL, EM_POPUP_SELECT_ONE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "90.filter.00/10.01", N_("Filter on Sen_der"), emfv_popup_filter_sender, NULL, NULL, EM_POPUP_SELECT_ONE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "90.filter.00/10.02", N_("Filter on Re_cipients"), emfv_popup_filter_recipients,  NULL, NULL, EM_POPUP_SELECT_ONE|EM_FOLDER_VIEW_SELECT_LISTONLY },
    { E_POPUP_ITEM, "90.filter.00/10.03", N_("Filter on _Mailing List"),
      emfv_popup_filter_mlist, NULL, NULL, EM_POPUP_SELECT_ONE|EM_POPUP_SELECT_MAILING_LIST|EM_FOLDER_VIEW_SELECT_LISTONLY },
};

static void
emfv_popup_labels_free(EPopup *ep, GSList *l, void *data)
{
    while (l) {
        GSList *n = l->next;
        EPopupItem *item = l->data;

        g_free(item->path);
        g_free(item);

        g_slist_free_1(l);
        l = n;
    }
}

static void
emfv_popup_items_free(EPopup *ep, GSList *items, void *data)
{
    g_slist_free(items);
}
       
static void
emfv_popup(EMFolderView *emfv, GdkEvent *event, int on_display)
{
    GSList *menus = NULL, *l, *label_list = NULL;
    GtkMenu *menu;
    EMPopup *emp;
    EMPopupTargetSelect *target;
    int i;

    /** @HookPoint-EMPopup: Message List Context Menu
     * @Id: org.gnome.evolution.mail.folderview.popup.select
     * @Type: EMPopup
     * @Target: EMPopupTargetSelect
     *
     * This is the context menu shown on the message list or over a message.
     */
    emp = em_popup_new("org.gnome.evolution.mail.folderview.popup");
    target = em_folder_view_get_popup_target(emfv, emp, on_display);

    for (i=0;i<sizeof(emfv_popup_items)/sizeof(emfv_popup_items[0]);i++)
        menus = g_slist_prepend(menus, &emfv_popup_items[i]);

    e_popup_add_items((EPopup *)emp, menus, NULL, emfv_popup_items_free, emfv);

    i = 1;
    if (!on_display) {
        for (l = mail_config_get_labels(); l; l = l->next) {
            EPopupItem *item;
            MailConfigLabel *label = l->data;
            GdkPixmap *pixmap;
            GdkColor colour;
            GdkGC *gc;
        
            item = g_malloc0(sizeof(*item));
            item->type = E_POPUP_IMAGE;
            item->path = g_strdup_printf("60.label.00/00.label.%02d", i++);
            item->label = label->name;
            item->activate = emfv_popup_label_set;
            item->user_data = label->tag;

            item->visible = EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_LISTONLY;

            gdk_color_parse(label->colour, &colour);
            gdk_color_alloc(gdk_colormap_get_system(), &colour);
        
            pixmap = gdk_pixmap_new(((GtkWidget *)emfv)->window, 16, 16, -1);
            gc = gdk_gc_new(((GtkWidget *)emfv)->window);
            gdk_gc_set_foreground(gc, &colour);
            gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, 16, 16);
            gdk_gc_unref(gc);

            item->image = gtk_image_new_from_pixmap(pixmap, NULL);
            gtk_widget_show(item->image);

            label_list = g_slist_prepend(label_list, item);
        }
    }

    e_popup_add_items((EPopup *)emp, label_list, NULL, emfv_popup_labels_free, emfv);

    menu = e_popup_create_menu_once((EPopup *)emp, (EPopupTarget *)target, 0);

    if (event == NULL || event->type == GDK_KEY_PRESS) {
        /* FIXME: menu pos function */
        gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, event ? event->key.time : gtk_get_current_event_time());
    } else {
        gtk_menu_popup(menu, NULL, NULL, NULL, NULL, event->button.button, event->button.time);
    }
}

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

/* Bonobo menu's */

/* a lot of stuff maps directly to the popup menu equivalent */
#define EMFV_MAP_CALLBACK(from, to)             \
static void                         \
from(BonoboUIComponent *uid, void *data, const char *path)  \
{                               \
    to(NULL, NULL, data);                   \
}

EMFV_MAP_CALLBACK(emfv_add_sender_addressbook, emfv_popup_add_sender)
EMFV_MAP_CALLBACK(emfv_message_apply_filters, emfv_popup_apply_filters)
EMFV_MAP_CALLBACK(emfv_message_filter_junk, emfv_popup_filter_junk)
EMFV_MAP_CALLBACK(emfv_message_copy, emfv_popup_copy)
EMFV_MAP_CALLBACK(emfv_message_move, emfv_popup_move)
EMFV_MAP_CALLBACK(emfv_message_forward, emfv_popup_forward)
EMFV_MAP_CALLBACK(emfv_message_reply_all, emfv_popup_reply_all)
EMFV_MAP_CALLBACK(emfv_message_reply_list, emfv_popup_reply_list)
EMFV_MAP_CALLBACK(emfv_message_reply_sender, emfv_popup_reply_sender)
EMFV_MAP_CALLBACK(emfv_message_mark_read, emfv_popup_mark_read)
EMFV_MAP_CALLBACK(emfv_message_mark_unread, emfv_popup_mark_unread)
EMFV_MAP_CALLBACK(emfv_message_mark_important, emfv_popup_mark_important)
EMFV_MAP_CALLBACK(emfv_message_mark_unimportant, emfv_popup_mark_unimportant)
EMFV_MAP_CALLBACK(emfv_message_mark_junk, emfv_popup_mark_junk)
EMFV_MAP_CALLBACK(emfv_message_mark_nojunk, emfv_popup_mark_nojunk)
EMFV_MAP_CALLBACK(emfv_message_delete, emfv_popup_delete)
EMFV_MAP_CALLBACK(emfv_message_undelete, emfv_popup_undelete)
EMFV_MAP_CALLBACK(emfv_message_followup_flag, emfv_popup_flag_followup)
/*EMFV_MAP_CALLBACK(emfv_message_followup_clear, emfv_popup_flag_clear)
  EMFV_MAP_CALLBACK(emfv_message_followup_completed, emfv_popup_flag_completed)*/
EMFV_MAP_CALLBACK(emfv_message_open, emfv_popup_open)
EMFV_MAP_CALLBACK(emfv_message_edit, emfv_popup_edit)
EMFV_MAP_CALLBACK(emfv_message_saveas, emfv_popup_saveas)
EMFV_MAP_CALLBACK(emfv_print_message, emfv_popup_print)
EMFV_MAP_CALLBACK(emfv_message_source, emfv_popup_source)

static void
emfv_empty_trash(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;
    
    em_utils_empty_trash (gtk_widget_get_toplevel ((GtkWidget *) emfv));
}


static void
emfv_edit_cut(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    if (GTK_WIDGET_HAS_FOCUS(emfv->preview->formathtml.html))
        em_format_html_display_cut(emfv->preview);
    else
        message_list_copy(emfv->list, TRUE);
}

static void
emfv_edit_copy(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    if (GTK_WIDGET_HAS_FOCUS(emfv->preview->formathtml.html))
        em_format_html_display_copy(emfv->preview);
    else
        message_list_copy(emfv->list, FALSE);
}

static void
emfv_edit_paste(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    message_list_paste(emfv->list);
}

static void
emfv_select_all_text(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    html_engine_select_all (((EMFormatHTML *)emfv->preview)->html->engine);
}

static void
emfv_mail_next(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    e_profile_event_emit("goto.next", "", 0);

    message_list_select(emfv->list, MESSAGE_LIST_SELECT_NEXT, 0, 0);
}

static void
emfv_mail_next_flagged(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;
    
    message_list_select(emfv->list, MESSAGE_LIST_SELECT_NEXT|MESSAGE_LIST_SELECT_WRAP, CAMEL_MESSAGE_FLAGGED, CAMEL_MESSAGE_FLAGGED);
}

static void
emfv_mail_next_unread(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    message_list_select(emfv->list, MESSAGE_LIST_SELECT_NEXT|MESSAGE_LIST_SELECT_WRAP, 0, CAMEL_MESSAGE_SEEN);
}

static void
emfv_mail_next_thread(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    message_list_select_next_thread(emfv->list);
}

static void
emfv_mail_previous(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    message_list_select(emfv->list, MESSAGE_LIST_SELECT_PREVIOUS, 0, 0);
}

static void
emfv_mail_previous_flagged(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    message_list_select(emfv->list, MESSAGE_LIST_SELECT_PREVIOUS|MESSAGE_LIST_SELECT_WRAP, CAMEL_MESSAGE_FLAGGED, CAMEL_MESSAGE_FLAGGED);
}

static void
emfv_mail_previous_unread(BonoboUIComponent *uid, void *data, const char *path)
{
    EMFolderView *emfv = data;

    message_list_select(emfv->list, MESSAGE_LIST_SELECT_PREVIOUS|MESSAGE_LIST_SELECT_WRAP, 0, CAMEL_MESSAGE_SEEN);
}

static void
emfv_message_forward_attached (BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;
    GPtrArray *uids;
    
    if (!em_utils_check_user_can_send_mail ((GtkWidget *) emfv))
        return;
    
    uids = message_list_get_selected (emfv->list);
    em_utils_forward_attached (emfv->folder, uids, emfv->folder_uri);
}

static void
emfv_message_forward_inline (BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;
    GPtrArray *uids;
    
    if (!em_utils_check_user_can_send_mail ((GtkWidget *) emfv))
        return;
    
    uids = message_list_get_selected (emfv->list);
    em_utils_forward_inline (emfv->folder, uids, emfv->folder_uri);
}

static void
emfv_message_forward_quoted (BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;
    GPtrArray *uids;
    
    if (!em_utils_check_user_can_send_mail ((GtkWidget *) emfv))
        return;
    
    uids = message_list_get_selected (emfv->list);
    em_utils_forward_quoted (emfv->folder, uids, emfv->folder_uri);
}

static void
emfv_message_redirect (BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;
    
    if (emfv->list->cursor_uid == NULL)
        return;
    
    if (!em_utils_check_user_can_send_mail ((GtkWidget *) emfv))
        return;
    
    em_utils_redirect_message_by_uid (emfv->folder, emfv->list->cursor_uid);
}

static void
emfv_message_post_reply (BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;
    
    if (emfv->list->cursor_uid == NULL)
        return;
    
    if (!em_utils_check_user_can_send_mail ((GtkWidget *) emfv))
        return;
    
    em_utils_post_reply_to_message_by_uid (emfv->folder, emfv->list->cursor_uid);
}

static void
emfv_message_reply(EMFolderView *emfv, int mode)
{
    char *selection_string;
    guint len;
    
    if (emfv->list->cursor_uid == NULL)
        return;
    
    if (!em_utils_check_user_can_send_mail ((GtkWidget *) emfv))
        return;

    selection_string = gtk_html_get_selection_html (((EMFormatHTML *)emfv->preview)->html, &len);
    if (selection_string && len) {
        CamelMimeMessage *msg, *src;
        struct _camel_header_raw *header;
        
        src = (CamelMimeMessage *)((EMFormat *)emfv->preview)->message;
        msg = camel_mime_message_new();
        
        /* need to strip content- headers */
        header = ((CamelMimePart *)src)->headers;
        while (header) {
            if (g_ascii_strncasecmp(header->name, "content-", 8) != 0)
                camel_medium_add_header((CamelMedium *)msg, header->name, header->value);
            header = header->next;
        }
        camel_mime_part_set_encoding((CamelMimePart *)msg, CAMEL_TRANSFER_ENCODING_8BIT);
        camel_mime_part_set_content((CamelMimePart *)msg,
                        selection_string, len, "text/html");
        em_utils_reply_to_message (emfv->folder, emfv->list->cursor_uid, msg, mode, NULL);
        camel_object_unref(msg);
    } else {
        em_utils_reply_to_message (emfv->folder, emfv->list->cursor_uid, NULL, mode, (EMFormat *)emfv->preview);
    }
}

static void
emfv_message_search(BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;

    em_format_html_display_search(emfv->preview);
}

static void
emfv_print_preview_message(BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;

    em_folder_view_print(emfv, TRUE);
}

static void
emfv_text_zoom_in(BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;

    if (emfv->preview)
        em_format_html_display_zoom_in(emfv->preview);
}

static void
emfv_text_zoom_out(BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;

    if (emfv->preview)
        em_format_html_display_zoom_out(emfv->preview);
}

static void
emfv_text_zoom_reset(BonoboUIComponent *uic, void *data, const char *path)
{
    EMFolderView *emfv = data;

    if (emfv->preview)
        em_format_html_display_zoom_reset(emfv->preview);
}

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

struct _filter_data {
    const char *source;
    char *uri;
    int type;
};

static void
filter_data_free (struct _filter_data *fdata)
{
    g_free (fdata->uri);
    g_free (fdata);
}

static void
filter_type_got_message (CamelFolder *folder, const char *uid, CamelMimeMessage *msg, void *user_data)
{
    struct _filter_data *data = user_data;
    
    if (msg)
        filter_gui_add_from_message (msg, data->source, data->type);
    
    filter_data_free (data);
}

static void
filter_type_uid (CamelFolder *folder, const char *uid, const char *source, int type)
{
    struct _filter_data *data;
    
    data = g_malloc0 (sizeof (*data));
    data->type = type;
    data->source = source;
    
    mail_get_message (folder, uid, filter_type_got_message, data, mail_thread_new);
}

static void
filter_type_current (EMFolderView *emfv, int type)
{
    const char *source;
    GPtrArray *uids;
    
    if (em_utils_folder_is_sent (emfv->folder, emfv->folder_uri)
        || em_utils_folder_is_outbox (emfv->folder, emfv->folder_uri))
        source = FILTER_SOURCE_OUTGOING;
    else
        source = FILTER_SOURCE_INCOMING;
    
    uids = message_list_get_selected (emfv->list);
    
    if (uids->len == 1)
        filter_type_uid (emfv->folder, (char *) uids->pdata[0], source, type);
    
    em_utils_uids_free (uids);
}

EMFV_MAP_CALLBACK(emfv_tools_filter_subject, emfv_popup_filter_subject)
EMFV_MAP_CALLBACK(emfv_tools_filter_sender, emfv_popup_filter_sender)
EMFV_MAP_CALLBACK(emfv_tools_filter_recipient, emfv_popup_filter_recipients)
EMFV_MAP_CALLBACK(emfv_tools_filter_mlist, emfv_popup_filter_mlist)

static void
vfolder_type_got_message (CamelFolder *folder, const char *uid, CamelMimeMessage *msg, void *user_data)
{
    struct _filter_data *data = user_data;
    
    if (msg)
        vfolder_gui_add_from_message (msg, data->type, data->uri);
    
    filter_data_free (data);
}

static void
emp_uri_popup_vfolder_sender(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    EMPopupTargetURI *t = (EMPopupTargetURI *)ep->target;
    CamelURL *url;  
    CamelInternetAddress *addr;
    
    url = camel_url_new(t->uri, NULL);
    if (url == NULL) {
        g_warning("cannot parse url '%s'", t->uri);
        return;
    }

    if (url->path && url->path[0]) {
        addr = camel_internet_address_new ();
        camel_address_decode (CAMEL_ADDRESS (addr), url->path);
        vfolder_gui_add_from_address (addr, AUTO_FROM, emfv->folder_uri);
        camel_object_unref (addr);
    }
    
    camel_url_free(url);    
    
}


static void
emp_uri_popup_vfolder_recipient(EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderView *emfv = data;
    EMPopupTargetURI *t = (EMPopupTargetURI *)ep->target;
    CamelURL *url;  
    CamelInternetAddress *addr;
    
    url = camel_url_new(t->uri, NULL);
    if (url == NULL) {
        g_warning("cannot parse url '%s'", t->uri);
        return;
    }

    if (url->path && url->path[0]) {
        addr = camel_internet_address_new ();
        camel_address_decode (CAMEL_ADDRESS (addr), url->path);
        vfolder_gui_add_from_address (addr, AUTO_TO, emfv->folder_uri);
        camel_object_unref (addr);
    }

    camel_url_free(url);    
}

static void
vfolder_type_uid (CamelFolder *folder, const char *uid, const char *uri, int type)
{
    struct _filter_data *data;
    
    data = g_malloc0 (sizeof (*data));
    data->type = type;
    data->uri = g_strdup (uri);
    
    mail_get_message (folder, uid, vfolder_type_got_message, data, mail_thread_new);
}

static void
vfolder_type_current (EMFolderView *emfv, int type)
{
    GPtrArray *uids;
    
    uids = message_list_get_selected (emfv->list);
    
    if (uids->len == 1)
        vfolder_type_uid (emfv->folder, (char *) uids->pdata[0], emfv->folder_uri, type);
    
    em_utils_uids_free (uids);
}

EMFV_MAP_CALLBACK(emfv_tools_vfolder_subject, emfv_popup_vfolder_subject)
EMFV_MAP_CALLBACK(emfv_tools_vfolder_sender, emfv_popup_vfolder_sender)
EMFV_MAP_CALLBACK(emfv_tools_vfolder_recipient, emfv_popup_vfolder_recipients)
EMFV_MAP_CALLBACK(emfv_tools_vfolder_mlist, emfv_popup_vfolder_mlist)

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

static BonoboUIVerb emfv_message_verbs[] = {
    BONOBO_UI_UNSAFE_VERB ("EmptyTrash", emfv_empty_trash),

    BONOBO_UI_UNSAFE_VERB ("EditCut", emfv_edit_cut),
    BONOBO_UI_UNSAFE_VERB ("EditCopy", emfv_edit_copy),
    BONOBO_UI_UNSAFE_VERB ("EditPaste", emfv_edit_paste),

    BONOBO_UI_UNSAFE_VERB ("SelectAllText", emfv_select_all_text),

    BONOBO_UI_UNSAFE_VERB ("MailNext", emfv_mail_next),
    BONOBO_UI_UNSAFE_VERB ("MailNextFlagged", emfv_mail_next_flagged),
    BONOBO_UI_UNSAFE_VERB ("MailNextUnread", emfv_mail_next_unread),
    BONOBO_UI_UNSAFE_VERB ("MailNextThread", emfv_mail_next_thread),
    BONOBO_UI_UNSAFE_VERB ("MailPrevious", emfv_mail_previous),
    BONOBO_UI_UNSAFE_VERB ("MailPreviousFlagged", emfv_mail_previous_flagged),
    BONOBO_UI_UNSAFE_VERB ("MailPreviousUnread", emfv_mail_previous_unread),

    BONOBO_UI_UNSAFE_VERB ("AddSenderToAddressbook", emfv_add_sender_addressbook),

    BONOBO_UI_UNSAFE_VERB ("MessageApplyFilters", emfv_message_apply_filters),
    BONOBO_UI_UNSAFE_VERB ("MessageFilterJunk", emfv_message_filter_junk),
    BONOBO_UI_UNSAFE_VERB ("MessageCopy", emfv_message_copy),
    BONOBO_UI_UNSAFE_VERB ("MessageDelete", emfv_message_delete),
    BONOBO_UI_UNSAFE_VERB ("MessageForward", emfv_message_forward),
    BONOBO_UI_UNSAFE_VERB ("MessageForwardAttached", emfv_message_forward_attached),
    BONOBO_UI_UNSAFE_VERB ("MessageForwardInline", emfv_message_forward_inline),
    BONOBO_UI_UNSAFE_VERB ("MessageForwardQuoted", emfv_message_forward_quoted),
    BONOBO_UI_UNSAFE_VERB ("MessageRedirect", emfv_message_redirect),
    BONOBO_UI_UNSAFE_VERB ("MessageMarkAsRead", emfv_message_mark_read),
    BONOBO_UI_UNSAFE_VERB ("MessageMarkAsUnRead", emfv_message_mark_unread),
    BONOBO_UI_UNSAFE_VERB ("MessageMarkAsImportant", emfv_message_mark_important),
    BONOBO_UI_UNSAFE_VERB ("MessageMarkAsUnimportant", emfv_message_mark_unimportant),
    BONOBO_UI_UNSAFE_VERB ("MessageMarkAsJunk", emfv_message_mark_junk),
    BONOBO_UI_UNSAFE_VERB ("MessageMarkAsNotJunk", emfv_message_mark_nojunk),
    BONOBO_UI_UNSAFE_VERB ("MessageFollowUpFlag", emfv_message_followup_flag),
    BONOBO_UI_UNSAFE_VERB ("MessageMove", emfv_message_move),
    BONOBO_UI_UNSAFE_VERB ("MessageOpen", emfv_message_open),
    BONOBO_UI_UNSAFE_VERB ("MessagePostReply", emfv_message_post_reply),
    BONOBO_UI_UNSAFE_VERB ("MessageReplyAll", emfv_message_reply_all),
    BONOBO_UI_UNSAFE_VERB ("MessageReplyList", emfv_message_reply_list),
    BONOBO_UI_UNSAFE_VERB ("MessageReplySender", emfv_message_reply_sender),
    BONOBO_UI_UNSAFE_VERB ("MessageEdit", emfv_message_edit),
    BONOBO_UI_UNSAFE_VERB ("MessageSaveAs", emfv_message_saveas),
    BONOBO_UI_UNSAFE_VERB ("MessageSearch", emfv_message_search),
    BONOBO_UI_UNSAFE_VERB ("MessageUndelete", emfv_message_undelete),

    BONOBO_UI_UNSAFE_VERB ("PrintMessage", emfv_print_message),
    BONOBO_UI_UNSAFE_VERB ("PrintPreviewMessage", emfv_print_preview_message),

    BONOBO_UI_UNSAFE_VERB ("TextZoomIn", emfv_text_zoom_in),
    BONOBO_UI_UNSAFE_VERB ("TextZoomOut", emfv_text_zoom_out),
    BONOBO_UI_UNSAFE_VERB ("TextZoomReset", emfv_text_zoom_reset),

    BONOBO_UI_UNSAFE_VERB ("ViewSource", emfv_message_source),

    BONOBO_UI_UNSAFE_VERB ("MailCompose", emfv_mail_compose),

    /* TODO: This stuff should just be 1 item that runs a wizard */
    BONOBO_UI_UNSAFE_VERB ("ToolsFilterMailingList", emfv_tools_filter_mlist),
    BONOBO_UI_UNSAFE_VERB ("ToolsFilterRecipient", emfv_tools_filter_recipient),
    BONOBO_UI_UNSAFE_VERB ("ToolsFilterSender", emfv_tools_filter_sender),
    BONOBO_UI_UNSAFE_VERB ("ToolsFilterSubject", emfv_tools_filter_subject),
    BONOBO_UI_UNSAFE_VERB ("ToolsVFolderMailingList", emfv_tools_vfolder_mlist),
    BONOBO_UI_UNSAFE_VERB ("ToolsVFolderRecipient", emfv_tools_vfolder_recipient),
    BONOBO_UI_UNSAFE_VERB ("ToolsVFolderSender", emfv_tools_vfolder_sender),
    BONOBO_UI_UNSAFE_VERB ("ToolsVFolderSubject", emfv_tools_vfolder_subject),

    BONOBO_UI_UNSAFE_VERB ("ViewLoadImages", emfv_view_load_images),
    /* ViewHeaders stuff is a radio */
    /* CaretMode is a toggle */

    BONOBO_UI_VERB_END
};
static EPixmap emfv_message_pixmaps[] = {
    E_PIXMAP ("/commands/EditCut", "stock_cut", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/EditCopy", "stock_copy", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/EditPaste", "stock_paste", E_ICON_SIZE_MENU),

    E_PIXMAP ("/commands/PrintMessage", "stock_print", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/PrintPreviewMessage", "stock_print-preview", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageDelete", "stock_delete", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageUndelete", "stock_undelete", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageCopy", "stock_mail-copy", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageMove", "stock_mail-move", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageReplyAll", "stock_mail-reply-to-all", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageReplySender", "stock_mail-reply", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageForward", "stock_mail-forward", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageApplyFilters", "stock_mail-filters-apply", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageFilterJunk", "stock_spam", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageSearch", "stock_search", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageSaveAs", "stock_save-as", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageMarkAsRead", "stock_mail-open", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageMarkAsUnRead", "stock_mail-unread", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageMarkAsImportant", "stock_mail-priority-high", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageMarkAsJunk", "stock_spam", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageMarkAsNotJunk", "stock_not-spam", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MessageFollowUpFlag", "stock_mail-flag-for-followup", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/ViewLoadImages", "stock_insert_image", E_ICON_SIZE_MENU),
    E_PIXMAP ("/commands/MailCompose", "stock_mail-compose", E_ICON_SIZE_MENU),

    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageReplySender", "stock_mail-reply", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageReplyAll", "stock_mail-reply-to-all", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageForward", "stock_mail-forward", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/PrintMessage", "stock_print", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageMove", "stock_mail-move", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageCopy", "stock_mail-copy", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageDelete", "stock_delete", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageMarkAsJunk", "stock_spam", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailMessageToolbar/MessageMarkAsNotJunk", "stock_not-spam", E_ICON_SIZE_LARGE_TOOLBAR),

    E_PIXMAP ("/Toolbar/MailNextButtons/MailNext", "stock_next", E_ICON_SIZE_LARGE_TOOLBAR),
    E_PIXMAP ("/Toolbar/MailNextButtons/MailPrevious", "stock_previous", E_ICON_SIZE_LARGE_TOOLBAR),

    E_PIXMAP_END
};

/* this is added to emfv->enable_map in :init() */
static const EMFolderViewEnable emfv_enable_map[] = {
    { "EditCut",                  EM_POPUP_SELECT_MANY },
    { "EditCopy",                 EM_FOLDER_VIEW_SELECT_SELECTION },
    { "EditPaste",                EM_POPUP_SELECT_FOLDER },
    { "SelectAllText",            EM_POPUP_SELECT_ONE },

    /* FIXME: should these be single-selection? */
    { "MailNext",                 EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_NEXT_MSG },
    { "MailNextFlagged",          EM_POPUP_SELECT_MANY },
    { "MailNextUnread",           EM_POPUP_SELECT_MANY },
    { "MailNextThread",           EM_POPUP_SELECT_MANY },
    { "MailPrevious",             EM_POPUP_SELECT_MANY|EM_FOLDER_VIEW_SELECT_PREV_MSG },
    { "MailPreviousFlagged",      EM_POPUP_SELECT_MANY },
    { "MailPreviousUnread",       EM_POPUP_SELECT_MANY },

    { "AddSenderToAddressbook",   EM_POPUP_SELECT_ADD_SENDER },

    { "MessageApplyFilters",      EM_POPUP_SELECT_MANY },
    { "MessageFilterJunk",        EM_POPUP_SELECT_MANY },
    { "MessageCopy",              EM_POPUP_SELECT_MANY },
    { "MessageDelete",            EM_POPUP_SELECT_MANY|EM_POPUP_SELECT_DELETE },
    { "MessageForward",           EM_POPUP_SELECT_MANY },
    { "MessageForwardAttached",   EM_POPUP_SELECT_MANY },
    { "MessageForwardInline",     EM_POPUP_SELECT_ONE },
    { "MessageForwardQuoted",     EM_POPUP_SELECT_ONE },
    { "MessageRedirect",          EM_POPUP_SELECT_ONE },
    { "MessageMarkAsRead",        EM_POPUP_SELECT_MANY|EM_POPUP_SELECT_MARK_READ },
    { "MessageMarkAsUnRead",      EM_POPUP_SELECT_MANY|EM_POPUP_SELECT_MARK_UNREAD },
    { "MessageMarkAsImportant",   EM_POPUP_SELECT_MANY|EM_POPUP_SELECT_MARK_IMPORTANT },
    { "MessageMarkAsUnimportant", EM_POPUP_SELECT_MANY|EM_POPUP_SELECT_MARK_UNIMPORTANT },
    { "MessageMarkAsJunk",        EM_POPUP_SELECT_MANY },
    { "MessageMarkAsNotJunk",     EM_POPUP_SELECT_MANY },
    { "MessageFollowUpFlag",      EM_POPUP_SELECT_MANY },
    { "MessageMove",              EM_POPUP_SELECT_MANY },
    { "MessageOpen",              EM_POPUP_SELECT_MANY },
    { "MessagePostReply",         EM_POPUP_SELECT_ONE },
    { "MessageReplyAll",          EM_POPUP_SELECT_ONE },
    { "MessageReplyList",         EM_POPUP_SELECT_ONE|EM_POPUP_SELECT_MAILING_LIST },
    { "MessageReplySender",       EM_POPUP_SELECT_ONE },
    { "MessageEdit",              EM_POPUP_SELECT_EDIT },
    { "MessageSaveAs",            EM_POPUP_SELECT_MANY },
    { "MessageSearch",            EM_POPUP_SELECT_ONE },
    { "MessageUndelete",          EM_POPUP_SELECT_MANY|EM_POPUP_SELECT_UNDELETE },
    { "PrintMessage",             EM_POPUP_SELECT_ONE },
    { "PrintPreviewMessage",      EM_POPUP_SELECT_ONE },

    { "TextZoomIn",           EM_POPUP_SELECT_ONE },
    { "TextZoomOut",          EM_POPUP_SELECT_ONE },
    { "TextZoomReset",        EM_POPUP_SELECT_ONE },

    { "ToolsFilterMailingList",   EM_POPUP_SELECT_ONE },
    { "ToolsFilterRecipient",     EM_POPUP_SELECT_ONE },
    { "ToolsFilterSender",        EM_POPUP_SELECT_ONE },
    { "ToolsFilterSubject",       EM_POPUP_SELECT_ONE },
    { "ToolsVFolderMailingList",  EM_POPUP_SELECT_ONE },
    { "ToolsVFolderRecipient",    EM_POPUP_SELECT_ONE },
    { "ToolsVFolderSender",       EM_POPUP_SELECT_ONE },
    { "ToolsVFolderSubject",      EM_POPUP_SELECT_ONE },

    { "ViewLoadImages",       EM_POPUP_SELECT_ONE },
    { "ViewSource",               EM_POPUP_SELECT_ONE },

    /* always enabled */
    { "MailStop", 0 },

    { NULL },

    /* always enabled

    { "ViewFullHeaders", IS_0MESSAGE, 0 },
    { "ViewNormal",      IS_0MESSAGE, 0 },
    { "ViewSource",      IS_0MESSAGE, 0 },
    { "CaretMode",       IS_0MESSAGE, 0 }, */
};

static void
emfv_enable_menus(EMFolderView *emfv)
{
    guint32 disable_mask;
    GString *name;
    GSList *l;

    if (emfv->uic == NULL)
        return;

    {
        if (emfv->menu) {
            if (emfv->folder) {
                EMMenuTargetSelect *t;

                t = em_menu_target_new_select(emfv->menu, emfv->folder, emfv->folder_uri, message_list_get_selected(emfv->list));
                e_menu_update_target((EMenu *)emfv->menu, t);
            } else {
                e_menu_update_target((EMenu *)emfv->menu, NULL);
            }
        }
    }

    if (emfv->folder) {
        EMPopup *emp = em_popup_new("dummy");
        EMPopupTargetSelect *t;

        t = em_folder_view_get_popup_target(emfv, emp, FALSE);
        disable_mask = t->target.mask;
        e_popup_target_free((EPopup *)emp, t);
        g_object_unref(emp);
    } else {
        disable_mask = ~0;
    }

    name = g_string_new("");
    for (l = emfv->enable_map; l; l = l->next) {
        EMFolderViewEnable *map = l->data;
        int i;

        for (i=0;map[i].name;i++) {
            int state = (map[i].mask & disable_mask) == 0;

            g_string_printf(name, "/commands/%s", map[i].name);
            bonobo_ui_component_set_prop(emfv->uic, name->str, "sensitive", state?"1":"0", NULL);
        }
    }

    g_string_free(name, TRUE);
}

/* must match em_format_mode_t order */
static const char * const emfv_display_styles[] = {
    "/commands/ViewNormal",
    "/commands/ViewFullHeaders",
    "/commands/ViewSource"
};

static void
emfv_view_mode(BonoboUIComponent *uic, const char *path, Bonobo_UIComponent_EventType type, const char *state, void *data)
{
    EMFolderView *emfv = data;
    int i;

    if (type != Bonobo_UIComponent_STATE_CHANGED)
        return;

    /* TODO: I don't like this stuff much, is there any way we can move listening for such events
       elsehwere?  Probably not I guess, unless there's a EMFolderViewContainer for bonobo usage
       of a folder view */

    i = state[0] != '0';

            em_format_set_mode((EMFormat *)emfv->preview, i);

            if (EM_FOLDER_VIEW_GET_CLASS (emfv)->update_message_style) {
                GConfClient *gconf = mail_config_get_gconf_client ();
                
                gconf_client_set_int (gconf, "/apps/evolution/mail/display/message_style", i, NULL);
            }
}

static void
emfv_caret_mode(BonoboUIComponent *uic, const char *path, Bonobo_UIComponent_EventType type, const char *state, void *data)
{
    EMFolderView *emfv = data;

    if (type != Bonobo_UIComponent_STATE_CHANGED)
        return;

    em_format_html_display_set_caret_mode(emfv->preview, state[0] != '0');

    gconf_client_set_bool(mail_config_get_gconf_client(), "/apps/evolution/mail/display/caret_mode", state[0] != '0', NULL);
}

static void
emfv_charset_changed(BonoboUIComponent *uic, const char *path, Bonobo_UIComponent_EventType type, const char *state, void *data)
{
    EMFolderView *emfv = data;

    if (type != Bonobo_UIComponent_STATE_CHANGED)
        return;

    /* menu items begin with "Charset-" = 8 characters */
    if (state[0] != '0' && strlen(path) > 8) {
        path += 8;
        /* default charset used in mail view */
        if (!strcmp(path, _("Default")))
            path = NULL;

        em_format_set_charset((EMFormat *)emfv->preview, path);
    }
}

static void
emfv_activate(EMFolderView *emfv, BonoboUIComponent *uic, int act)
{
    struct _EMFolderViewPrivate *p = emfv->priv;

    if (act) {
        em_format_mode_t style;
        gboolean state;
        GSList *l;

        emfv->uic = uic;

        for (l = emfv->ui_files;l;l = l->next)
            bonobo_ui_util_set_ui(uic, PREFIX, (char *)l->data, emfv->ui_app_name, NULL);

        bonobo_ui_component_add_verb_list_with_data(uic, emfv_message_verbs, emfv);
        e_pixmaps_update(uic, emfv_message_pixmaps);

        /* must do plugin menu's after main ones because of bonobo bustedness */
        if (emfv->menu)
            e_menu_activate((EMenu *)emfv->menu, uic, act);

        state = emfv->preview->caret_mode;
        bonobo_ui_component_set_prop(uic, "/commands/CaretMode", "state", state?"1":"0", NULL);
        bonobo_ui_component_add_listener(uic, "CaretMode", emfv_caret_mode, emfv);

        style = ((EMFormat *)emfv->preview)->mode?EM_FORMAT_ALLHEADERS:EM_FORMAT_NORMAL;
        bonobo_ui_component_set_prop(uic, emfv_display_styles[style], "state", style?"1":"0", NULL);
        /*      bonobo_ui_component_add_listener(uic, "ViewNormal", emfv_view_mode, emfv); */
        bonobo_ui_component_add_listener(uic, "ViewFullHeaders", emfv_view_mode, emfv);
        /*      bonobo_ui_component_add_listener(uic, "ViewSource", emfv_view_mode, emfv); */
        em_format_set_mode((EMFormat *)emfv->preview, style);
        
        if (emfv->folder && !em_utils_folder_is_sent(emfv->folder, emfv->folder_uri))
            bonobo_ui_component_set_prop(uic, "/commands/MessageEdit", "sensitive", "0", NULL);
        
        /* default charset used in mail view */
        e_charset_picker_bonobo_ui_populate (uic, "/menu/View", _("Default"), emfv_charset_changed, emfv);

        emfv_enable_menus(emfv);
        if (emfv->statusbar_active)
            bonobo_ui_component_set_translate (uic, "/", "<status><item name=\"main\"/></status>", NULL);

        /* We need to set this up to get the right view options for the message-list, even if we're not showing it */
        if (emfv->folder)
            emfv_setup_view_instance(emfv);
    } else {
        const BonoboUIVerb *v;

        if (emfv->menu)
            e_menu_activate((EMenu *)emfv->menu, uic, act);

        /* TODO: Should this just rm /? */
        for (v = &emfv_message_verbs[0]; v->cname; v++)
            bonobo_ui_component_remove_verb(uic, v->cname);

        if (p->view_instance) {
            g_object_unref(p->view_instance);
            p->view_instance = NULL;
        }

        if (p->view_menus) {
            g_object_unref(p->view_menus);
            p->view_menus = NULL;
        }

        if (emfv->folder)
            mail_sync_folder(emfv->folder, NULL, NULL);

        emfv->uic = NULL;
    }
}

struct _print_data {
    EMFolderView *emfv;

    GnomePrintConfig *config;
    int preview;
    CamelFolder *folder;
    char *uid;
};

static void
emfv_print_response(GtkWidget *w, int resp, struct _print_data *data)
{
    EMFormatHTMLPrint *print;

    switch (resp) {
    case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
        data->preview = TRUE;
    case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
        print = em_format_html_print_new();
        em_format_set_session((EMFormat *)print, ((EMFormat *)data->emfv->preview)->session);
        em_format_html_print_message(print, (EMFormatHTML *)data->emfv->preview, data->config, data->folder, data->uid, data->preview);
        g_object_unref(print);
        break;
    }

    if (w)
        gtk_widget_destroy(w);

    g_object_unref(data->emfv);
    e_print_save_config (data->config);
    g_object_unref(data->config);
    camel_object_unref(data->folder);
    g_free(data->uid);
    g_free(data);
}

int em_folder_view_print(EMFolderView *emfv, int preview)
{
    struct _print_data *data;
    GPtrArray *uids;

    if (emfv->folder == NULL)
        return 0;

    uids = message_list_get_selected(emfv->list);
    if (uids->len != 1) {
        message_list_free_uids(emfv->list, uids);
        return 0;
    }

    data = g_malloc0(sizeof(*data));
    data->emfv = emfv;
    g_object_ref(emfv);
    data->config = e_print_load_config ();
    data->preview = preview;
    data->folder = emfv->folder;
    camel_object_ref(data->folder);
    data->uid = g_strdup(uids->pdata[0]);
    message_list_free_uids(emfv->list, uids);

    if (preview) {
        emfv_print_response(NULL, GNOME_PRINT_DIALOG_RESPONSE_PREVIEW, data);
    } else {
        GtkDialog *dialog = (GtkDialog *)e_print_get_dialog_with_config (_("Print Message"), GNOME_PRINT_DIALOG_COPIES, data->config);
        
        gtk_dialog_set_default_response(dialog, GNOME_PRINT_DIALOG_RESPONSE_PRINT);
        e_dialog_set_transient_for ((GtkWindow *) dialog, (GtkWidget *) emfv);
        g_signal_connect(dialog, "response", G_CALLBACK(emfv_print_response), data);
        gtk_widget_show((GtkWidget *)dialog);
    }

    return 0;
}

EMPopupTargetSelect *
em_folder_view_get_popup_target(EMFolderView *emfv, EMPopup *emp, int on_display)
{
    EMPopupTargetSelect *t;

    t = em_popup_target_new_select(emp, emfv->folder, emfv->folder_uri, message_list_get_selected(emfv->list));
    t->target.widget = (GtkWidget *)emfv;

    if (emfv->list->threaded)
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_THREADED;

    if (message_list_hidden(emfv->list) != 0)
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_HIDDEN;

    if (message_list_can_select(emfv->list, MESSAGE_LIST_SELECT_NEXT, 0, 0))
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_NEXT_MSG;

    if (message_list_can_select(emfv->list, MESSAGE_LIST_SELECT_PREVIOUS, 0, 0))
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_PREV_MSG;

    if (on_display)
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_DISPLAY;
    else
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_LISTONLY;

    if (gtk_html_command (((EMFormatHTML *)emfv->preview)->html, "is-selection-active"))
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_SELECTION;
    else
        t->target.mask &= ~EM_FOLDER_VIEW_SELECT_NOSELECTION;

    /* See bug #54770 */
    if (!emfv->hide_deleted)
        t->target.mask &= ~EM_POPUP_SELECT_DELETE;
    
    return t;
}

void
em_folder_view_set_statusbar (EMFolderView *emfv, gboolean statusbar)
{
    g_return_if_fail (emfv);
    
    emfv->statusbar_active = statusbar;

    if (statusbar && emfv->uic)
        bonobo_ui_component_set_translate (emfv->uic, "/",
                           "<status><item name=\"main\"/></status>", NULL);
}

void
em_folder_view_set_hide_deleted(EMFolderView *emfv, gboolean status)
{
    if (emfv->folder && (emfv->folder->folder_flags & CAMEL_FOLDER_IS_TRASH))
        status = FALSE;

    emfv->hide_deleted = status;

    if (emfv->folder) {
        message_list_set_hidedeleted(emfv->list, status);
        g_signal_emit(emfv, signals[EMFV_CHANGED], 0);
    }
}

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

struct mst_t {
    EMFolderView *emfv;
    char *uid;
};

static void
mst_free (struct mst_t *mst)
{
    mst->emfv->priv->seen_id = 0;
    
    g_free (mst->uid);
    g_free (mst);
}

static int
do_mark_seen (gpointer user_data)
{
    struct mst_t *mst = user_data;
    EMFolderView *emfv = mst->emfv;
    MessageList *list = emfv->list;
    
    if (mst->uid && list->cursor_uid && !strcmp (mst->uid, list->cursor_uid))
        emfv_set_seen (emfv, mst->uid);
    
    return FALSE;
}

static void
emfv_list_done_message_selected(CamelFolder *folder, const char *uid, CamelMimeMessage *msg, void *data)
{
    EMFolderView *emfv = data;
    EMEvent *eme;
    EMEventTargetMessage *target;
    
    if (emfv->preview == NULL) {
        emfv->priv->nomarkseen = FALSE;
        emfv_enable_menus(emfv);
        g_object_unref (emfv);
        return;

    }

    e_profile_event_emit("goto.loaded", emfv->displayed_uid, 0);
        
    /** @Event: message.reading
     * @Title: Viewing a message
     * @Target: EMEventTargetMessage
     * 
     * message.reading is emitted whenever a user views a message.
     */
    /* TODO: do we emit a message.reading with no message when we're looking at nothing or don't care? */
    eme = em_event_peek();
    target = em_event_target_new_message(eme, folder, msg, uid, 0);
    e_event_emit((EEvent *)eme, "message.reading", (EEventTarget *)target);

    em_format_format((EMFormat *)emfv->preview, folder, uid, msg);

    if (emfv->priv->seen_id)
        g_source_remove(emfv->priv->seen_id);
    
    if (msg && emfv->mark_seen && !emfv->priv->nomarkseen) {
        if (emfv->mark_seen_timeout > 0) {
            struct mst_t *mst;
            
            mst = g_new (struct mst_t, 1);
            mst->emfv = emfv;
            mst->uid = g_strdup (uid);
            
            emfv->priv->seen_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, emfv->mark_seen_timeout,
                                 (GSourceFunc)do_mark_seen, mst, (GDestroyNotify)mst_free);
        } else {
            emfv_set_seen (emfv, uid);
        }
    }
    
    emfv->priv->nomarkseen = FALSE;
    emfv_enable_menus(emfv);
    g_object_unref (emfv);
}

static gboolean
emfv_message_selected_timeout(void *data)
{
    EMFolderView *emfv = data;

    if (emfv->priv->selected_uid) {
        if (emfv->displayed_uid == NULL || strcmp(emfv->displayed_uid, emfv->priv->selected_uid) != 0) {
            g_free(emfv->displayed_uid);
            emfv->displayed_uid = emfv->priv->selected_uid;
            emfv->priv->selected_uid = NULL;
            g_object_ref (emfv);
            /* TODO: we should manage our own thread stuff, would make cancelling outstanding stuff easier */
            e_profile_event_emit("goto.load", emfv->displayed_uid, 0);
            mail_get_message(emfv->folder, emfv->displayed_uid, emfv_list_done_message_selected, emfv, mail_thread_queued);
        } else {
            e_profile_event_emit("goto.empty", "", 0);
            g_free(emfv->priv->selected_uid);
            emfv->priv->selected_uid = NULL;
        }
    } else {
        e_profile_event_emit("goto.empty", "", 0);
        g_free(emfv->displayed_uid);
        emfv->displayed_uid = NULL;
        em_format_format((EMFormat *)emfv->preview, NULL, NULL, NULL);
        emfv->priv->nomarkseen = FALSE;
    }

    emfv->priv->selected_id = 0;

    return FALSE;
}

static void
emfv_list_message_selected(MessageList *ml, const char *uid, EMFolderView *emfv)
{
    e_profile_event_emit("goto.listuid", uid, 0);

    if (emfv->preview_active) {
        if (emfv->priv->selected_id != 0)
            g_source_remove(emfv->priv->selected_id);

        emfv->priv->selected_id = g_timeout_add(100, emfv_message_selected_timeout, emfv);

        g_free(emfv->priv->selected_uid);
        emfv->priv->selected_uid = g_strdup(uid);
    }

    emfv_enable_menus(emfv);

    g_signal_emit(emfv, signals[EMFV_CHANGED], 0);
}

static void
emfv_list_built(MessageList *ml, EMFolderView *emfv)
{
    if (!emfv->priv->destroyed) {
        emfv_enable_menus(emfv);
        g_signal_emit(emfv, signals[EMFV_LOADED], 0);
    }
}

static void
emfv_list_double_click(ETree *tree, gint row, ETreePath path, gint col, GdkEvent *event, EMFolderView *emfv)
{
    /* Ignore double-clicks on columns that handle thier own state */
    if (MESSAGE_LIST_COLUMN_IS_ACTIVE (col))
        return;

    em_folder_view_open_selected(emfv);
}

static int
emfv_list_right_click(ETree *tree, gint row, ETreePath path, gint col, GdkEvent *event, EMFolderView *emfv)
{
    emfv_popup(emfv, event, FALSE);

    return TRUE;
}

static int