aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui
diff options
context:
space:
mode:
authorMatthew Barnes <mbarnes@redhat.com>2009-05-05 05:01:29 +0800
committerMatthew Barnes <mbarnes@redhat.com>2009-05-05 05:01:29 +0800
commitf23d7c5fa097a3df630dd36a3c19e659280c726a (patch)
treec6f948da48b752a707ed278411149a6b7eaa75a2 /calendar/gui
parentba2cf0efba5fa98421088b7bb7d0a364aefaf042 (diff)
downloadgsoc2013-evolution-f23d7c5fa097a3df630dd36a3c19e659280c726a.tar.gz
gsoc2013-evolution-f23d7c5fa097a3df630dd36a3c19e659280c726a.tar.zst
gsoc2013-evolution-f23d7c5fa097a3df630dd36a3c19e659280c726a.zip
Mark CompEditor as changed when attachments are added or removed.
Diffstat (limited to 'calendar/gui')
-rw-r--r--calendar/gui/dialogs/comp-editor.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/calendar/gui/dialogs/comp-editor.c b/calendar/gui/dialogs/comp-editor.c
index 68b240cc25..4af998eb26 100644
--- a/calendar/gui/dialogs/comp-editor.c
+++ b/calendar/gui/dialogs/comp-editor.c
@@ -182,6 +182,14 @@ enum {
static guint comp_editor_signals[LAST_SIGNAL] = { 0 };
+static void
+attachment_store_changed_cb (CompEditor *editor)
+{
+ /* Mark the editor as changed so it prompts about unsaved
+ changes on close */
+ comp_editor_set_changed (editor, TRUE);
+}
+
static GSList *
get_attachment_list (CompEditor *editor)
{
@@ -1513,6 +1521,7 @@ comp_editor_init (CompEditor *editor)
{
CompEditorPrivate *priv;
EAttachmentView *view;
+ EAttachmentStore *store;
GdkDragAction drag_actions;
GtkTargetList *target_list;
GtkTargetEntry *targets;
@@ -1646,6 +1655,18 @@ comp_editor_init (CompEditor *editor)
gtk_window_set_type_hint (
GTK_WINDOW (editor), GDK_WINDOW_TYPE_HINT_NORMAL);
+
+ /* Listen for attachment store changes. */
+
+ store = e_attachment_view_get_store (view);
+
+ g_signal_connect_swapped (
+ store, "row-deleted",
+ G_CALLBACK (attachment_store_changed_cb), editor);
+
+ g_signal_connect_swapped (
+ store, "row-inserted",
+ G_CALLBACK (attachment_store_changed_cb), editor);
}
static gboolean
@@ -1698,14 +1719,6 @@ prompt_and_save_changes (CompEditor *editor, gboolean send)
}
}
-static void
-attachment_store_changed_cb (CompEditor *editor)
-{
- /* Mark the editor as changed so it prompts about unsaved
- changes on close */
- comp_editor_set_changed (editor, TRUE);
-}
-
/* Menu callbacks */
static void
a id='n225' href='#n225'>225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Rodrigo Moya <rodrigo@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>
#include <time.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gdk/gdkkeysyms.h>
#include <libedataserver/e-time-utils.h>
#include <e-util/e-util.h>
#include <e-util/e-alert-dialog.h>
#include <e-util/e-extensible.h>
#include <e-util/e-selection.h>
#include <e-util/e-datetime-format.h>
#include <e-util/e-dialog-utils.h>
#include <e-util/e-icon-factory.h>
#include <libecal/e-cal-time-util.h>
#include <libecal/e-cal-component.h>
#include <misc/e-selectable.h>
#include <shell/e-shell.h>

#include "common/authentication.h"
#include "comp-util.h"
#include "ea-calendar.h"
#include "e-cal-model-calendar.h"
#include "e-calendar-view.h"
#include "itip-utils.h"
#include "dialogs/comp-editor-util.h"
#include "dialogs/delete-comp.h"
#include "dialogs/delete-error.h"
#include "dialogs/event-editor.h"
#include "dialogs/send-comp.h"
#include "dialogs/cancel-comp.h"
#include "dialogs/recur-comp.h"
#include "dialogs/select-source-dialog.h"
#include "print.h"
#include "goto.h"
#include "misc.h"

struct _ECalendarViewPrivate {
    /* The GnomeCalendar we are associated to */
    GnomeCalendar *calendar;

    /* The calendar model we are monitoring */
    ECalModel *model;

    gchar *default_category;
    gint time_divisions;

    GtkTargetList *copy_target_list;
    GtkTargetList *paste_target_list;
};

enum {
    PROP_0,
    PROP_COPY_TARGET_LIST,
    PROP_MODEL,
    PROP_PASTE_TARGET_LIST,
    PROP_TIME_DIVISIONS
};

/* FIXME Why are we emitting these event signals here? Can't the model just be listened to? */
/* Signal IDs */
enum {
    POPUP_EVENT,
    SELECTION_CHANGED,
    SELECTED_TIME_CHANGED,
    TIMEZONE_CHANGED,
    EVENT_CHANGED,
    EVENT_ADDED,
    USER_CREATED,
    OPEN_EVENT,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static void calendar_view_selectable_init (ESelectableInterface *interface);

G_DEFINE_ABSTRACT_TYPE_WITH_CODE (
    ECalendarView, e_calendar_view, GTK_TYPE_TABLE,
    G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL)
    G_IMPLEMENT_INTERFACE (E_TYPE_SELECTABLE, calendar_view_selectable_init));

static void
calendar_view_add_retract_data (ECalComponent *comp,
                                const gchar *retract_comment,
                                CalObjModType mod)
{
    icalcomponent *icalcomp = NULL;
    icalproperty *icalprop = NULL;

    icalcomp = e_cal_component_get_icalcomponent (comp);
    if (retract_comment && *retract_comment)
        icalprop = icalproperty_new_x (retract_comment);
    else
        icalprop = icalproperty_new_x ("0");
    icalproperty_set_x_name (icalprop, "X-EVOLUTION-RETRACT-COMMENT");
    icalcomponent_add_property (icalcomp, icalprop);

    if (mod == CALOBJ_MOD_ALL)
        icalprop = icalproperty_new_x ("All");
    else
        icalprop = icalproperty_new_x ("This");
    icalproperty_set_x_name (icalprop, "X-EVOLUTION-RECUR-MOD");
    icalcomponent_add_property (icalcomp, icalprop);
}

static gboolean
calendar_view_check_for_retract (ECalComponent *comp,
                                 ECal *client)
{
    ECalComponentOrganizer organizer;
    const gchar *strip;
    gchar *email = NULL;
    gboolean ret_val;

    if (!e_cal_component_has_attendees (comp))
        return FALSE;

    if (!e_cal_get_save_schedules (client))
        return FALSE;

    e_cal_component_get_organizer (comp, &organizer);
    strip = itip_strip_mailto (organizer.value);

    ret_val =
        e_cal_get_cal_address (client, &email, NULL) &&
        (g_ascii_strcasecmp (email, strip) == 0);

    g_free (email);

    return ret_val;
}

static void
calendar_view_delete_event (ECalendarView *cal_view,
                            ECalendarViewEvent *event)
{
    ECalModel *model;
    ECalComponent *comp;
    ECalComponentVType vtype;
    gboolean delete = TRUE;
    GError *error = NULL;

    if (!is_comp_data_valid (event))
        return;

    model = e_calendar_view_get_model (cal_view);

    comp = e_cal_component_new ();
    e_cal_component_set_icalcomponent (comp, icalcomponent_new_clone (event->comp_data->icalcomp));
    vtype = e_cal_component_get_vtype (comp);

    /*FIXME remove it once the we dont set the recurrence id for all the generated instances */
    if (!e_cal_get_static_capability (event->comp_data->client, CAL_STATIC_CAPABILITY_RECURRENCES_NO_MASTER))
        e_cal_component_set_recurid (comp, NULL);

    /*FIXME Retract should be moved to Groupwise features plugin */
    if (calendar_view_check_for_retract (comp, event->comp_data->client)) {
        gchar *retract_comment = NULL;
        gboolean retract = FALSE;

        delete = prompt_retract_dialog (comp, &retract_comment, GTK_WIDGET (cal_view), &retract);
        if (retract) {
            GList *users = NULL;
            icalcomponent *icalcomp = NULL, *mod_comp = NULL;

            calendar_view_add_retract_data (
                comp, retract_comment, CALOBJ_MOD_ALL);
            icalcomp = e_cal_component_get_icalcomponent (comp);
            icalcomponent_set_method (icalcomp, ICAL_METHOD_CANCEL);
            if (!e_cal_send_objects (event->comp_data->client, icalcomp, &users,
                        &mod_comp, &error)) {
                delete_error_dialog (error, E_CAL_COMPONENT_EVENT);
                g_clear_error (&error);
                error = NULL;
            } else {

                if (mod_comp)
                    icalcomponent_free (mod_comp);

                if (users) {
                    g_list_foreach (users, (GFunc) g_free, NULL);
                    g_list_free (users);
                }
            }
        }
    } else if (e_cal_model_get_confirm_delete (model))
        delete = delete_component_dialog (
            comp, FALSE, 1, vtype, GTK_WIDGET (cal_view));

    if (delete) {
        const gchar *uid;
        gchar *rid = NULL;

        if ((itip_organizer_is_user (comp, event->comp_data->client) || itip_sentby_is_user (comp, event->comp_data->client))
            && cancel_component_dialog ((GtkWindow *) gtk_widget_get_toplevel (GTK_WIDGET (cal_view)),
                        event->comp_data->client,
                        comp, TRUE))
            itip_send_comp (E_CAL_COMPONENT_METHOD_CANCEL, comp,
                    event->comp_data->client, NULL, NULL, NULL, TRUE, FALSE);

        e_cal_component_get_uid (comp, &uid);
        if (!uid || !*uid) {
            g_object_unref (comp);
            return;
        }
        rid = e_cal_component_get_recurid_as_string (comp);
        if (e_cal_util_component_is_instance (event->comp_data->icalcomp) || e_cal_util_component_has_recurrences (event->comp_data->icalcomp))
            e_cal_remove_object_with_mod (event->comp_data->client, uid,
                rid, CALOBJ_MOD_ALL, &error);
        else
            e_cal_remove_object (event->comp_data->client, uid, &error);

        delete_error_dialog (error, E_CAL_COMPONENT_EVENT);
        g_clear_error (&error);
        g_free (rid);
    }

    g_object_unref (comp);
}

static void
calendar_view_set_model (ECalendarView *calendar_view,
                         ECalModel *model)
{
    g_return_if_fail (calendar_view->priv->model == NULL);
    g_return_if_fail (E_IS_CAL_MODEL (model));

    calendar_view->priv->model = g_object_ref (model);
}

static void
calendar_view_set_property (GObject *object,
                            guint property_id,
                            const GValue *value,
                            GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_MODEL:
            calendar_view_set_model (
                E_CALENDAR_VIEW (object),
                g_value_get_object (value));
            return;

        case PROP_TIME_DIVISIONS:
            e_calendar_view_set_time_divisions (
                E_CALENDAR_VIEW (object),
                g_value_get_int (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
calendar_view_get_property (GObject *object,
                            guint property_id,
                            GValue *value,
                            GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_COPY_TARGET_LIST:
            g_value_set_boxed (
                value, e_calendar_view_get_copy_target_list (
                E_CALENDAR_VIEW (object)));
            return;

        case PROP_MODEL:
            g_value_set_object (
                value, e_calendar_view_get_model (
                E_CALENDAR_VIEW (object)));
            return;

        case PROP_PASTE_TARGET_LIST:
            g_value_set_boxed (
                value, e_calendar_view_get_paste_target_list (
                E_CALENDAR_VIEW (object)));
            return;

        case PROP_TIME_DIVISIONS:
            g_value_set_int (
                value, e_calendar_view_get_time_divisions (
                E_CALENDAR_VIEW (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
calendar_view_dispose (GObject *object)
{
    ECalendarViewPrivate *priv;

    priv = E_CALENDAR_VIEW (object)->priv;

    if (priv->model != NULL) {
        g_signal_handlers_disconnect_matched (
            priv->model, G_SIGNAL_MATCH_DATA,
            0, 0, NULL, NULL, object);
        g_object_unref (priv->model);
        priv->model = NULL;
    }

    if (priv->copy_target_list != NULL) {
        gtk_target_list_unref (priv->copy_target_list);
        priv->copy_target_list = NULL;
    }

    if (priv->paste_target_list != NULL) {
        gtk_target_list_unref (priv->paste_target_list);
        priv->paste_target_list = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_calendar_view_parent_class)->dispose (object);
}

static void
calendar_view_finalize (GObject *object)
{
    ECalendarViewPrivate *priv;

    priv = E_CALENDAR_VIEW (object)->priv;

    g_free (priv->default_category);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_calendar_view_parent_class)->finalize (object);
}

static void
calendar_view_constructed (GObject *object)
{
    /* Do this after calendar_view_init() so extensions can query
     * the GType accurately.  See GInstanceInitFunc documentation
     * for details of the problem. */
    e_extensible_load_extensions (E_EXTENSIBLE (object));

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_calendar_view_parent_class)->constructed (object);
}

static void
calendar_view_update_actions (ESelectable *selectable,
                              EFocusTracker *focus_tracker,
                              GdkAtom *clipboard_targets,
                              gint n_clipboard_targets)
{
    ECalendarView *view;
    GtkAction *action;
    GtkTargetList *target_list;
    GList *list, *iter;
    gboolean can_paste = FALSE;
    gboolean sources_are_editable = TRUE;
    gboolean recurring = FALSE;
    gboolean sensitive;
    const gchar *tooltip;
    gint n_selected;
    gint ii;

    view = E_CALENDAR_VIEW (selectable);

    list = e_calendar_view_get_selected_events (view);
    n_selected = g_list_length (list);

    for (iter = list; iter != NULL; iter = iter->next) {
        ECalendarViewEvent *event = iter->data;
        ECal *client;
        icalcomponent *icalcomp;
        gboolean read_only;

        if (event == NULL || event->comp_data == NULL)
            continue;

        client = event->comp_data->client;
        icalcomp = event->comp_data->icalcomp;

        e_cal_is_read_only (client, &read_only, NULL);
        sources_are_editable &= !read_only;

        recurring |=
            e_cal_util_component_is_instance (icalcomp) ||
            e_cal_util_component_has_recurrences (icalcomp);
    }

    g_list_free (list);

    target_list = e_selectable_get_paste_target_list (selectable);
    for (ii = 0; ii < n_clipboard_targets && !can_paste; ii++)
        can_paste = gtk_target_list_find (
            target_list, clipboard_targets[ii], NULL);

    action = e_focus_tracker_get_cut_clipboard_action (focus_tracker);
    sensitive = (n_selected > 0) && sources_are_editable;
    tooltip = _("Cut selected events to the clipboard");
    gtk_action_set_sensitive (action, sensitive);
    gtk_action_set_tooltip (action, tooltip);

    action = e_focus_tracker_get_copy_clipboard_action (focus_tracker);
    sensitive = (n_selected > 0);
    tooltip = _("Copy selected events to the clipboard");
    gtk_action_set_sensitive (action, sensitive);
    gtk_action_set_tooltip (action, tooltip);

    action = e_focus_tracker_get_paste_clipboard_action (focus_tracker);
    sensitive = sources_are_editable && can_paste;
    tooltip = _("Paste events from the clipboard");
    gtk_action_set_sensitive (action, sensitive);
    gtk_action_set_tooltip (action, tooltip);

    action = e_focus_tracker_get_delete_selection_action (focus_tracker);
    sensitive = (n_selected > 0) && sources_are_editable && !recurring;
    tooltip = _("Delete selected events");
    gtk_action_set_sensitive (action, sensitive);
    gtk_action_set_tooltip (action, tooltip);
}

static void
calendar_view_cut_clipboard (ESelectable *selectable)
{
    ECalendarView *cal_view;
    GList *selected, *l;
    const gchar *uid;

    cal_view = E_CALENDAR_VIEW (selectable);

    selected = e_calendar_view_get_selected_events (cal_view);
    if (!selected)
        return;

#if 0  /* KILL-BONOBO */
    e_calendar_view_set_status_message (cal_view, _("Deleting selected objects"), -1);
#endif

    e_selectable_copy_clipboard (selectable);

    for (l = selected; l != NULL; l = l->next) {
        ECalComponent *comp;
        ECalendarViewEvent *event = (ECalendarViewEvent *) l->data;
        GError *error = NULL;

        if (!event)
            continue;

        if (!is_comp_data_valid (event))
            continue;

        comp = e_cal_component_new ();
        e_cal_component_set_icalcomponent (comp, icalcomponent_new_clone (event->comp_data->icalcomp));

        if ((itip_organizer_is_user (comp, event->comp_data->client) || itip_sentby_is_user (comp, event->comp_data->client))
            && cancel_component_dialog ((GtkWindow *) gtk_widget_get_toplevel (GTK_WIDGET (cal_view)),
                        event->comp_data->client, comp, TRUE))
            itip_send_comp (E_CAL_COMPONENT_METHOD_CANCEL, comp,
                    event->comp_data->client, NULL, NULL, NULL, TRUE, FALSE);

        e_cal_component_get_uid (comp, &uid);
        if (e_cal_component_is_instance (comp)) {
            gchar *rid = NULL;
            icalcomponent *icalcomp;

            /* when cutting detached instances, only cut that instance */
            rid = e_cal_component_get_recurid_as_string (comp);
            if (e_cal_get_object (event->comp_data->client, uid, rid, &icalcomp, NULL)) {
                e_cal_remove_object_with_mod (event->comp_data->client, uid,
                                  rid, CALOBJ_MOD_THIS,
                                  &error);
                icalcomponent_free (icalcomp);
            } else
                e_cal_remove_object_with_mod (event->comp_data->client, uid, NULL,
                        CALOBJ_MOD_ALL, &error);
            g_free (rid);
        } else
            e_cal_remove_object (event->comp_data->client, uid, &error);
        delete_error_dialog (error, E_CAL_COMPONENT_EVENT);

        g_clear_error (&error);

        g_object_unref (comp);
    }

#if 0  /* KILL-BONOBO */
    e_calendar_view_set_status_message (cal_view, NULL, -1);
#endif

    g_list_free (selected);
}

static void
add_related_timezones (icalcomponent *des_icalcomp, icalcomponent *src_icalcomp, ECal *client)
{
    icalproperty_kind look_in[] = {
        ICAL_DTSTART_PROPERTY,
        ICAL_DTEND_PROPERTY,
        ICAL_NO_PROPERTY
    };
    gint i;

    g_return_if_fail (des_icalcomp != NULL);
    g_return_if_fail (src_icalcomp != NULL);
    g_return_if_fail (client != NULL);

    for (i = 0; look_in[i] != ICAL_NO_PROPERTY; i++) {
        icalproperty *prop = icalcomponent_get_first_property (src_icalcomp, look_in[i]);

        if (prop) {
            icalparameter *par = icalproperty_get_first_parameter (prop, ICAL_TZID_PARAMETER);

            if (par) {
                const gchar *tzid = icalparameter_get_tzid (par);

                if (tzid) {
                    GError *error = NULL;
                    icaltimezone *zone = NULL;

                    if (!e_cal_get_timezone (client, tzid, &zone, &error)) {
                        g_warning ("%s: Cannot get timezone for '%s'. %s", G_STRFUNC, tzid, error ? error->message : "");
                        if (error)
                            g_error_free (error);
                    } else if (zone &&
                        icalcomponent_get_timezone (des_icalcomp, icaltimezone_get_tzid (zone)) == NULL) {
                        /* do not duplicate timezones in the component */
                        icalcomponent *vtz_comp;

                        vtz_comp = icaltimezone_get_component (zone);
                        if (vtz_comp)
                            icalcomponent_add_component (des_icalcomp, icalcomponent_new_clone (vtz_comp));
                    }
                }
            }
        }
    }
}

static void
calendar_view_copy_clipboard (ESelectable *selectable)
{
    ECalendarView *cal_view;
    GList *selected, *l;
    gchar *comp_str;
    icalcomponent *vcal_comp;
    icalcomponent *new_icalcomp;
    ECalendarViewEvent *event;
    GtkClipboard *clipboard;

    cal_view = E_CALENDAR_VIEW (selectable);

    selected = e_calendar_view_get_selected_events (cal_view);
    if (!selected)
        return;

    /* create top-level VCALENDAR component and add VTIMEZONE's */
    vcal_comp = e_cal_util_new_top_level ();
    for (l = selected; l != NULL; l = l->next) {
        event = (ECalendarViewEvent *) l->data;

        if (event && is_comp_data_valid (event)) {
            e_cal_util_add_timezones_from_component (vcal_comp, event->comp_data->icalcomp);

            add_related_timezones (vcal_comp, event->comp_data->icalcomp, event->comp_data->client);
        }
    }

    for (l = selected; l != NULL; l = l->next) {
        event = (ECalendarViewEvent *) l->data;

        if (!is_comp_data_valid (event))
            continue;

        new_icalcomp = icalcomponent_new_clone (event->comp_data->icalcomp);

        /* remove RECURRENCE-IDs from copied objects */
        if (e_cal_util_component_is_instance (new_icalcomp)) {
            icalproperty *prop;

            prop = icalcomponent_get_first_property (new_icalcomp, ICAL_RECURRENCEID_PROPERTY);
            if (prop)
                icalcomponent_remove_property (new_icalcomp, prop);
        }
        icalcomponent_add_component (vcal_comp, new_icalcomp);
    }

    comp_str = icalcomponent_as_ical_string_r (vcal_comp);

    /* copy the VCALENDAR to the clipboard */
    clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
    e_clipboard_set_calendar (clipboard, comp_str, -1);
    gtk_clipboard_store (clipboard);

    /* free memory */
    icalcomponent_free (vcal_comp);
    g_free (comp_str);
    g_list_free (selected);
}

static void
clipboard_get_calendar_data (ECalendarView *cal_view,
                             const gchar *text)
{
    icalcomponent *icalcomp;
    icalcomponent_kind kind;
    time_t selected_time_start, selected_time_end;
    icaltimezone *default_zone;
    ECal *client;
    gboolean in_top_canvas;

    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    if (!text || !*text)
        return;

    icalcomp = icalparser_parse_string ((const gchar *) text);
    if (!icalcomp)
        return;

    default_zone = e_cal_model_get_timezone (cal_view->priv->model);
    client = e_cal_model_get_default_client (cal_view->priv->model);

    /* check the type of the component */
    /* FIXME An error dialog if we return? */
    kind = icalcomponent_isa (icalcomp);
    if (kind != ICAL_VCALENDAR_COMPONENT && kind != ICAL_VEVENT_COMPONENT)
        return;

#if 0  /* KILL-BONOBO */
    e_calendar_view_set_status_message (cal_view, _("Updating objects"), -1);
#endif
    e_calendar_view_get_selected_time_range (cal_view, &selected_time_start, &selected_time_end);

    if ((selected_time_end - selected_time_start) == 60 * 60 * 24)
        in_top_canvas = TRUE;
    else
        in_top_canvas = FALSE;

    if (kind == ICAL_VCALENDAR_COMPONENT) {
        icalcomponent *subcomp;

        /* add timezones first, to have them ready */
        for (subcomp = icalcomponent_get_first_component (icalcomp, ICAL_VTIMEZONE_COMPONENT);
             subcomp;
             subcomp = icalcomponent_get_next_component (icalcomp, ICAL_VTIMEZONE_COMPONENT)) {
            icaltimezone *zone;
            GError *error = NULL;

            zone = icaltimezone_new ();
            icaltimezone_set_component (zone, subcomp);
            if (!e_cal_add_timezone (client, zone, &error)) {
                icalproperty *tzidprop = icalcomponent_get_first_property (subcomp, ICAL_TZID_PROPERTY);

                g_warning ("%s: Add zone '%s' failed. %s", G_STRFUNC, tzidprop ? icalproperty_get_tzid (tzidprop) : "???", error ? error->message : "");
                if (error)
                    g_error_free (error);
            }

            icaltimezone_free (zone, 1);
        }

        for (subcomp = icalcomponent_get_first_component (icalcomp, ICAL_VEVENT_COMPONENT);
             subcomp;
             subcomp = icalcomponent_get_next_component (icalcomp, ICAL_VEVENT_COMPONENT)) {
            if (e_cal_util_component_has_recurrences (subcomp)) {
                icalproperty *icalprop = icalcomponent_get_first_property (subcomp, ICAL_RRULE_PROPERTY);
                if (icalprop)
                    icalproperty_remove_parameter_by_name (icalprop, "X-EVOLUTION-ENDDATE");
            }

            e_calendar_view_add_event (cal_view, client, selected_time_start, default_zone, subcomp, in_top_canvas);
        }

        icalcomponent_free (icalcomp);
    } else {
        e_calendar_view_add_event (cal_view, client, selected_time_start, default_zone, icalcomp, in_top_canvas);
    }

#if 0  /* KILL-BONOBO */
    e_calendar_view_set_status_message (cal_view, NULL, -1);
#endif
}

static void
calendar_view_paste_clipboard (ESelectable *selectable)
{
    ECalendarView *cal_view;
    GtkClipboard *clipboard;

    cal_view = E_CALENDAR_VIEW (selectable);

    clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);

    /* Paste text into an event being edited. */
    if (gtk_clipboard_wait_is_text_available (clipboard)) {
        ECalendarViewClass *class;

        class = E_CALENDAR_VIEW_GET_CLASS (cal_view);
        g_return_if_fail (class->paste_text != NULL);

        class->paste_text (cal_view);

    /* Paste iCalendar data into the view. */
    } else if (e_clipboard_wait_is_calendar_available (clipboard)) {
        gchar *calendar_source;

        calendar_source = e_clipboard_wait_for_calendar (clipboard);
        clipboard_get_calendar_data (cal_view, calendar_source);
        g_free (calendar_source);
    }
}

static void
calendar_view_delete_selection (ESelectable *selectable)
{
    ECalendarView *cal_view;
    GList *selected, *iter;

    cal_view = E_CALENDAR_VIEW (selectable);

    selected = e_calendar_view_get_selected_events (cal_view);

    for (iter = selected; iter != NULL; iter = iter->next) {
        ECalendarViewEvent *event = iter->data;

        /* XXX Why would this ever be NULL? */
        if (event == NULL)
            continue;

        calendar_view_delete_event (cal_view, event);
    }

    g_list_free (selected);
}

static void
e_calendar_view_class_init (ECalendarViewClass *class)
{
    GObjectClass *object_class;
    GtkBindingSet *binding_set;

    g_type_class_add_private (class, sizeof (ECalendarViewPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = calendar_view_set_property;
    object_class->get_property = calendar_view_get_property;
    object_class->dispose = calendar_view_dispose;
    object_class->finalize = calendar_view_finalize;
    object_class->constructed = calendar_view_constructed;

    class->selection_changed = NULL;
    class->selected_time_changed = NULL;
    class->event_changed = NULL;
    class->event_added = NULL;
    class->user_created = NULL;

    class->get_selected_events = NULL;
    class->get_selected_time_range = NULL;
    class->set_selected_time_range = NULL;
    class->get_visible_time_range = NULL;
    class->update_query = NULL;
    class->open_event = e_calendar_view_open_event;
    class->paste_text = NULL;

    /* Inherited from ESelectableInterface */
    g_object_class_override_property (
        object_class,
        PROP_COPY_TARGET_LIST,
        "copy-target-list");

    g_object_class_install_property (
        object_class,
        PROP_MODEL,
        g_param_spec_object (
            "model",
            "Model",
            NULL,
            E_TYPE_CAL_MODEL,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY));

    /* Inherited from ESelectableInterface */
    g_object_class_override_property (
        object_class,
        PROP_PASTE_TARGET_LIST,
        "paste-target-list");

    g_object_class_install_property (
        object_class,
        PROP_TIME_DIVISIONS,
        g_param_spec_int (
            "time-divisions",
            "Time Divisions",
            NULL,
            G_MININT,
            G_MAXINT,
            30,
            G_PARAM_READWRITE));

    signals[POPUP_EVENT] = g_signal_new (
        "popup-event",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_FIRST,
        G_STRUCT_OFFSET (ECalendarViewClass, popup_event),
        NULL, NULL,
        g_cclosure_marshal_VOID__BOXED,
        G_TYPE_NONE, 1,
        GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);

    signals[SELECTION_CHANGED] = g_signal_new (
        "selection-changed",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ECalendarViewClass, selection_changed),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    signals[SELECTED_TIME_CHANGED] = g_signal_new (
        "selected-time-changed",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ECalendarViewClass, selected_time_changed),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    signals[TIMEZONE_CHANGED] = g_signal_new (
        "timezone-changed",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ECalendarViewClass, timezone_changed),
        NULL, NULL,
        e_marshal_VOID__POINTER_POINTER,
        G_TYPE_NONE, 2,
        G_TYPE_POINTER,
        G_TYPE_POINTER);

    signals[EVENT_CHANGED] = g_signal_new (
        "event-changed",
        G_TYPE_FROM_CLASS (object_class),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (ECalendarViewClass, event_changed),
        NULL, NULL,
        g_cclosure_marshal_VOID__POINTER,
        G_TYPE_NONE, 1,
        G_TYPE_POINTER);

    signals[EVENT_ADDED] = g_signal_new (
        "event-added",
        G_TYPE_FROM_CLASS (object_class),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (ECalendarViewClass, event_added),
        NULL, NULL,
        g_cclosure_marshal_VOID__POINTER,
        G_TYPE_NONE, 1,
        G_TYPE_POINTER);

    signals[USER_CREATED] = g_signal_new (
        "user-created",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ECalendarViewClass, user_created),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    signals[OPEN_EVENT] = g_signal_new (
        "open-event",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (ECalendarViewClass, open_event),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    /* Key bindings */

    binding_set = gtk_binding_set_by_class (class);

    gtk_binding_entry_add_signal (
        binding_set, GDK_KEY_o, GDK_CONTROL_MASK, "open-event", 0);

    /* init the accessibility support for e_day_view */
    e_cal_view_a11y_init ();
}

static void
e_calendar_view_init (ECalendarView *calendar_view)
{
    GtkTargetList *target_list;

    calendar_view->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar_view, E_TYPE_CALENDAR_VIEW, ECalendarViewPrivate);

    /* Set this early to avoid a divide-by-zero during init. */
    calendar_view->priv->time_divisions = 30;

    target_list = gtk_target_list_new (NULL, 0);
    e_target_list_add_calendar_targets (target_list, 0);
    calendar_view->priv->copy_target_list = target_list;

    target_list = gtk_target_list_new (NULL, 0);
    e_target_list_add_calendar_targets (target_list, 0);
    calendar_view->priv->paste_target_list = target_list;
}

static void
calendar_view_selectable_init (ESelectableInterface *interface)
{
    interface->update_actions = calendar_view_update_actions;
    interface->cut_clipboard = calendar_view_cut_clipboard;
    interface->copy_clipboard = calendar_view_copy_clipboard;
    interface->paste_clipboard = calendar_view_paste_clipboard;
    interface->delete_selection = calendar_view_delete_selection;
}

void
e_calendar_view_popup_event (ECalendarView *calendar_view,
                             GdkEventButton *event)
{
    g_return_if_fail (E_IS_CALENDAR_VIEW (calendar_view));
    g_return_if_fail (event != NULL);

    g_signal_emit (calendar_view, signals[POPUP_EVENT], 0, event);
}

void
e_calendar_view_add_event (ECalendarView *cal_view, ECal *client, time_t dtstart,
              icaltimezone *default_zone, icalcomponent *icalcomp, gboolean in_top_canvas)
{
    ECalComponent *comp;
    struct icaltimetype itime, old_dtstart, old_dtend;
    time_t tt_start, tt_end, new_dtstart = 0;
    struct icaldurationtype ic_dur, ic_oneday;
    gchar *uid;
    gint start_offset, end_offset;
    gboolean all_day_event = FALSE;
    GnomeCalendarViewType view_type;
    GError *error = NULL;

    start_offset = 0;
    end_offset = 0;

    old_dtstart = icalcomponent_get_dtstart (icalcomp);
    tt_start = icaltime_as_timet (old_dtstart);
    old_dtend = icalcomponent_get_dtend (icalcomp);
    tt_end = icaltime_as_timet (old_dtend);
    ic_dur = icaldurationtype_from_int (tt_end - tt_start);

    if (icaldurationtype_as_int (ic_dur) > 60*60*24) {
        /* This is a long event */
        start_offset = old_dtstart.hour * 60 + old_dtstart.minute;
        end_offset = old_dtstart.hour * 60 + old_dtend.minute;
    }

    ic_oneday = icaldurationtype_null_duration ();
    ic_oneday.days = 1;

    view_type = gnome_calendar_get_view (cal_view->priv->calendar);

    switch (view_type) {
    case GNOME_CAL_DAY_VIEW:
    case GNOME_CAL_WORK_WEEK_VIEW:
        if (start_offset == 0 && end_offset == 0 && in_top_canvas)
            all_day_event = TRUE;

        if (all_day_event) {
            ic_dur = ic_oneday;
        } else if (icaldurationtype_as_int (ic_dur) >= 60*60*24
                && !in_top_canvas) {
            /* copy & paste from top canvas to main canvas */
            gint time_divisions;

            time_divisions = e_calendar_view_get_time_divisions (cal_view);
            ic_dur = icaldurationtype_from_int (time_divisions * 60);
        }

        if (in_top_canvas)
            new_dtstart = dtstart + start_offset * 60;
        else
            new_dtstart = dtstart;
        break;
    case GNOME_CAL_WEEK_VIEW:
    case GNOME_CAL_MONTH_VIEW:
    case GNOME_CAL_LIST_VIEW:
        if (old_dtstart.is_date && old_dtend.is_date
            && memcmp (&ic_dur, &ic_oneday, sizeof (ic_dur)) == 0)
            all_day_event = TRUE;
        else {
            icaltimetype new_time = icaltime_from_timet_with_zone (dtstart, FALSE, default_zone);

            new_time.hour = old_dtstart.hour;
            new_time.minute = old_dtstart.minute;
            new_time.second = old_dtstart.second;

            new_dtstart = icaltime_as_timet_with_zone (new_time, old_dtstart.zone ? old_dtstart.zone : default_zone);
        }
        break;
    default:
        g_return_if_reached ();
    }

    itime = icaltime_from_timet_with_zone (new_dtstart, FALSE, old_dtstart.zone ? old_dtstart.zone : default_zone);
    /* set the timezone properly */
    itime.zone = old_dtstart.zone ? old_dtstart.zone : default_zone;
    if (all_day_event)
        itime.is_date = TRUE;
    icalcomponent_set_dtstart (icalcomp, itime);

    itime.is_date = FALSE;
    itime = icaltime_add (itime, ic_dur);
    if (all_day_event)
        itime.is_date = TRUE;
    icalcomponent_set_dtend (icalcomp, itime);

    /* FIXME The new uid stuff can go away once we actually set it in the backend */
    uid = e_cal_component_gen_uid ();
    comp = e_cal_component_new ();
    e_cal_component_set_icalcomponent (
        comp, icalcomponent_new_clone (icalcomp));
    e_cal_component_set_uid (comp, uid);
    g_free (uid);

    e_cal_component_commit_sequence (comp);

    uid = NULL;
    if (e_cal_create_object (client, e_cal_component_get_icalcomponent (comp), &uid, &error)) {
        gboolean strip_alarms = TRUE;

        if (uid) {
            e_cal_component_set_uid (comp, uid);
            g_free (uid);
        }

        if ((itip_organizer_is_user (comp, client) || itip_sentby_is_user (comp, client)) &&
            send_component_dialog ((GtkWindow *) gtk_widget_get_toplevel (GTK_WIDGET (cal_view)),
                       client, comp, TRUE, &strip_alarms, NULL)) {
            itip_send_comp (E_CAL_COMPONENT_METHOD_REQUEST, comp,
                client, NULL, NULL, NULL, strip_alarms, FALSE);
        }
    } else {
        g_message (G_STRLOC ": Could not create the object! %s", error ? error->message : "");
        if (error)
            g_error_free (error);
    }

    g_object_unref (comp);
}

GnomeCalendar *
e_calendar_view_get_calendar (ECalendarView *cal_view)
{
    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), NULL);

    return cal_view->priv->calendar;
}

void
e_calendar_view_set_calendar (ECalendarView *cal_view, GnomeCalendar *calendar)
{
    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    cal_view->priv->calendar = calendar;
}

ECalModel *
e_calendar_view_get_model (ECalendarView *cal_view)
{
    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), NULL);

    return cal_view->priv->model;
}

icaltimezone *
e_calendar_view_get_timezone (ECalendarView *cal_view)
{
    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), NULL);
    return e_cal_model_get_timezone (cal_view->priv->model);
}

void
e_calendar_view_set_timezone (ECalendarView *cal_view, icaltimezone *zone)
{
    icaltimezone *old_zone;

    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    old_zone = e_cal_model_get_timezone (cal_view->priv->model);
    if (old_zone == zone)
        return;

    e_cal_model_set_timezone (cal_view->priv->model, zone);
    g_signal_emit (G_OBJECT (cal_view), signals[TIMEZONE_CHANGED], 0,
               old_zone, zone);
}

const gchar *
e_calendar_view_get_default_category (ECalendarView *cal_view)
{
    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), NULL);

    return cal_view->priv->default_category;
}

/**
 * e_calendar_view_set_default_category
 * @cal_view: A calendar view.
 * @category: Default category name or NULL for no category.
 *
 * Sets the default category that will be used when creating new calendar
 * components from the given calendar view.
 */
void
e_calendar_view_set_default_category (ECalendarView *cal_view,
                                      const gchar *category)
{
    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    g_free (cal_view->priv->default_category);
    cal_view->priv->default_category = g_strdup (category);
}

GtkTargetList *
e_calendar_view_get_copy_target_list (ECalendarView *cal_view)
{
    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), NULL);

    return cal_view->priv->copy_target_list;
}

GtkTargetList *
e_calendar_view_get_paste_target_list (ECalendarView *cal_view)
{
    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), NULL);

    return cal_view->priv->paste_target_list;
}

gint
e_calendar_view_get_time_divisions (ECalendarView *cal_view)
{
    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), 0);

    return cal_view->priv->time_divisions;
}

void
e_calendar_view_set_time_divisions (ECalendarView *cal_view,
                                    gint time_divisions)
{
    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    cal_view->priv->time_divisions = time_divisions;

    g_object_notify (G_OBJECT (cal_view), "time-divisions");
}

GList *
e_calendar_view_get_selected_events (ECalendarView *cal_view)
{
    ECalendarViewClass *class;

    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), NULL);

    class = E_CALENDAR_VIEW_GET_CLASS (cal_view);
    g_return_val_if_fail (class->get_selected_events != NULL, NULL);

    return class->get_selected_events (cal_view);
}

gboolean
e_calendar_view_get_selected_time_range (ECalendarView *cal_view,
                                         time_t *start_time,
                                         time_t *end_time)
{
    ECalendarViewClass *class;

    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), FALSE);

    class = E_CALENDAR_VIEW_GET_CLASS (cal_view);
    g_return_val_if_fail (class->get_selected_time_range != NULL, FALSE);

    return class->get_selected_time_range (cal_view, start_time, end_time);
}

void
e_calendar_view_set_selected_time_range (ECalendarView *cal_view,
                                         time_t start_time,
                                         time_t end_time)
{
    ECalendarViewClass *class;

    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    /* Not all views implement this, so return silently. */
    class = E_CALENDAR_VIEW_GET_CLASS (cal_view);
    if (class->set_selected_time_range == NULL)
        return;

    class->set_selected_time_range (cal_view, start_time, end_time);
}

gboolean
e_calendar_view_get_visible_time_range (ECalendarView *cal_view,
                                        time_t *start_time,
                                        time_t *end_time)
{
    ECalendarViewClass *class;

    g_return_val_if_fail (E_IS_CALENDAR_VIEW (cal_view), FALSE);

    class = E_CALENDAR_VIEW_GET_CLASS (cal_view);
    g_return_val_if_fail (class->get_visible_time_range != NULL, FALSE);

    return class->get_visible_time_range (cal_view, start_time, end_time);
}

void
e_calendar_view_update_query (ECalendarView *cal_view)
{
    ECalendarViewClass *class;

    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    class = E_CALENDAR_VIEW_GET_CLASS (cal_view);
    g_return_if_fail (class->update_query != NULL);

    class->update_query (cal_view);
}

void
e_calendar_view_delete_selected_occurrence (ECalendarView *cal_view)
{
    GList *selected;
    ECalModel *model;
    ECalComponent *comp;
    ECalendarViewEvent *event;
    ECalComponentVType vtype;
    gboolean delete = TRUE;
    GError *error = NULL;

    model = e_calendar_view_get_model (cal_view);

    selected = e_calendar_view_get_selected_events (cal_view);
    if (!selected)
        return;
    event = (ECalendarViewEvent *) selected->data;
    if (!is_comp_data_valid (event))
        return;

    comp = e_cal_component_new ();
    e_cal_component_set_icalcomponent (comp, icalcomponent_new_clone (event->comp_data->icalcomp));
    vtype = e_cal_component_get_vtype (comp);

    /*FIXME Retract should be moved to Groupwise features plugin */
    if (calendar_view_check_for_retract (comp, event->comp_data->client)) {
        gchar *retract_comment = NULL;
        gboolean retract = FALSE;

        delete = prompt_retract_dialog (comp, &retract_comment, GTK_WIDGET (cal_view), &retract);
        if (retract) {
            GList *users = NULL;
            icalcomponent *icalcomp = NULL, *mod_comp = NULL;

            calendar_view_add_retract_data (
                comp, retract_comment, CALOBJ_MOD_THIS);
            icalcomp = e_cal_component_get_icalcomponent (comp);
            icalcomponent_set_method (icalcomp, ICAL_METHOD_CANCEL);
            if (!e_cal_send_objects (event->comp_data->client, icalcomp, &users,
                        &mod_comp, &error)) {
                delete_error_dialog (error, E_CAL_COMPONENT_EVENT);
                g_clear_error (&error);
                error = NULL;
            } else {
                if (mod_comp)
                    icalcomponent_free (mod_comp);
                if (users) {
                    g_list_foreach (users, (GFunc) g_free, NULL);
                    g_list_free (users);
                }
            }
        }
    } else if (e_cal_model_get_confirm_delete (model))
        delete = delete_component_dialog (
            comp, FALSE, 1, vtype, GTK_WIDGET (cal_view));

    if (delete) {
        const gchar *uid;
        gchar *rid = NULL;
        ECalComponentDateTime dt;
        icaltimezone *zone = NULL;
        gboolean is_instance = FALSE;

        e_cal_component_get_uid (comp, &uid);
        e_cal_component_get_dtstart (comp, &dt);
        is_instance = e_cal_component_is_instance (comp);

        if (dt.tzid) {
            GError *error = NULL;

            e_cal_get_timezone (event->comp_data->client, dt.tzid, &zone, &error);
            if (error) {
                zone = e_calendar_view_get_timezone (cal_view);
                g_clear_error (&error);
            }
        } else
            zone = e_calendar_view_get_timezone (cal_view);

        if (is_instance)
            rid = e_cal_component_get_recurid_as_string (comp);

        e_cal_component_free_datetime (&dt);

        if ((itip_organizer_is_user (comp, event->comp_data->client) || itip_sentby_is_user (comp, event->comp_data->client))
                && cancel_component_dialog ((GtkWindow *) gtk_widget_get_toplevel (GTK_WIDGET (cal_view)),
                    event->comp_data->client,
                    comp, TRUE) && !e_cal_get_save_schedules (event->comp_data->client)) {
            if (!e_cal_component_is_instance (comp)) {
                ECalComponentRange range;

                /* set the recurrence ID of the object we send */
                range.type = E_CAL_COMPONENT_RANGE_SINGLE;
                e_cal_component_get_dtstart (comp, &range.datetime);
                range.datetime.value->is_date = 1;
                e_cal_component_set_recurid (comp, &range);

                e_cal_component_free_datetime (&range.datetime);
            }
            itip_send_comp (E_CAL_COMPONENT_METHOD_CANCEL, comp, event->comp_data->client, NULL, NULL, NULL, TRUE, FALSE);
        }

        if (is_instance)
            e_cal_remove_object_with_mod (event->comp_data->client, uid, rid, CALOBJ_MOD_THIS, &error);
        else {
            struct icaltimetype instance_rid;

            instance_rid = icaltime_from_timet_with_zone (event->comp_data->instance_start,
                    TRUE, zone ? zone : icaltimezone_get_utc_timezone ());
            e_cal_util_remove_instances (event->comp_data->icalcomp, instance_rid, CALOBJ_MOD_THIS);
            e_cal_modify_object (event->comp_data->client, event->comp_data->icalcomp, CALOBJ_MOD_THIS,
                    &error);
        }

        delete_error_dialog (error, E_CAL_COMPONENT_EVENT);
        g_clear_error (&error);
        g_free (rid);
    }

    /* free memory */
    g_list_free (selected);
    g_object_unref (comp);
}

void
e_calendar_view_open_event (ECalendarView *cal_view)
{
    GList *selected;

    selected = e_calendar_view_get_selected_events (cal_view);
    if (selected) {
        ECalendarViewEvent *event = (ECalendarViewEvent *) selected->data;
        if (event && is_comp_data_valid (event))
            e_calendar_view_edit_appointment (cal_view, event->comp_data->client, event->comp_data->icalcomp, EDIT_EVENT_AUTODETECT);

        g_list_free (selected);
    }
}

/**
 * e_calendar_view_new_appointment_for
 * @cal_view: A calendar view.
 * @dtstart: A Unix time_t that marks the beginning of the appointment.
 * @dtend: A Unix time_t that marks the end of the appointment.
 * @all_day: If TRUE, the dtstart and dtend are expanded to cover
 * the entire day, and the event is set to TRANSPARENT.
 * @meeting: Whether the appointment is a meeting or not.
 *
 * Opens an event editor dialog for a new appointment.
 */
void
e_calendar_view_new_appointment_for (ECalendarView *cal_view,
                     time_t dtstart, time_t dtend,
                     gboolean all_day,
                     gboolean meeting)
{
    ECalendarViewPrivate *priv;
    struct icaltimetype itt;
    ECalComponentDateTime dt;
    ECalComponent *comp;
    icalcomponent *icalcomp;
    ECalComponentTransparency transparency;
    ECal *default_client = NULL;
    gpointer parent;
    guint32 flags = 0;
    gboolean readonly = FALSE;

    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    parent = gtk_widget_get_toplevel (GTK_WIDGET (cal_view));
    parent = gtk_widget_is_toplevel (parent) ? parent : NULL;

    priv = cal_view->priv;

    default_client = e_cal_model_get_default_client (priv->model);

    if (!default_client || e_cal_get_load_state (default_client) != E_CAL_LOAD_LOADED) {
        g_warning ("Default client not loaded \n");
        return;
    }

    if (e_cal_is_read_only (default_client, &readonly, NULL) && readonly) {
        GtkWidget *widget;

        widget = e_alert_dialog_new_for_args (parent, "calendar:prompt-read-only-cal", e_source_peek_name (e_cal_get_source (default_client)), NULL);

        g_signal_connect ((GtkDialog *)widget, "response", G_CALLBACK (gtk_widget_destroy),
                  widget);
        gtk_widget_show (widget);
        return;
    }

    dt.value = &itt;
    if (all_day)
        dt.tzid = NULL;
    else
        dt.tzid = icaltimezone_get_tzid (e_cal_model_get_timezone (cal_view->priv->model));

    icalcomp = e_cal_model_create_component_with_defaults (priv->model, all_day);
    comp = e_cal_component_new ();
    e_cal_component_set_icalcomponent (comp, icalcomp);

    /* DTSTART, DTEND */
    itt = icaltime_from_timet_with_zone (dtstart, FALSE, e_cal_model_get_timezone (cal_view->priv->model));
    if (all_day) {
        itt.hour = itt.minute = itt.second = 0;
        itt.is_date = TRUE;
    }
    e_cal_component_set_dtstart (comp, &dt);

    itt = icaltime_from_timet_with_zone (dtend, FALSE, e_cal_model_get_timezone (cal_view->priv->model));
    if (all_day) {
        /* We round it up to the end of the day, unless it is
           already set to midnight */
        if (itt.hour != 0 || itt.minute != 0 || itt.second != 0) {
            icaltime_adjust (&itt, 1, 0, 0, 0);
        }
        itt.hour = itt.minute = itt.second = 0;
        itt.is_date = TRUE;
    }
    e_cal_component_set_dtend (comp, &dt);

    /* TRANSPARENCY */
    transparency = all_day ? E_CAL_COMPONENT_TRANSP_TRANSPARENT
        : E_CAL_COMPONENT_TRANSP_OPAQUE;
    e_cal_component_set_transparency (comp, transparency);

    /* CATEGORY */
    e_cal_component_set_categories (comp, priv->default_category);

    /* edit the object */
    e_cal_component_commit_sequence (comp);

    flags |= COMP_EDITOR_NEW_ITEM;
    if (meeting) {
        flags |= COMP_EDITOR_MEETING;
        flags |= COMP_EDITOR_USER_ORG;
    }

    e_calendar_view_open_event_with_flags (cal_view, default_client,
            icalcomp, flags);

    g_object_unref (comp);
}

/**
 * e_calendar_view_new_appointment_full
 * @param cal_view: A calendar view.
 * @param all_day: Whether create all day event or not.
 * @param meeting: This is a meeting or an appointment.
 * @param no_past_date: Don't create event in past date, use actual date instead (if TRUE).
 *
 * Opens an event editor dialog for a new appointment. The appointment's
 * start and end times are set to the currently selected time range in
 * the calendar view.
 *
 * When the selection is for all day and we don't need @all_day event,
 * then this do a rounding to the actual hour for actual day (today) and
 * to the 'day begins' from preferences in other selected day.
 */
void
e_calendar_view_new_appointment_full (ECalendarView *cal_view,
                                      gboolean all_day,
                                      gboolean meeting,
                                      gboolean no_past_date)
{
    ECalModel *model;
    time_t dtstart, dtend, now;
    gboolean do_rounding = FALSE;

    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    model = e_calendar_view_get_model (cal_view);

    now = time (NULL);

    if (!e_calendar_view_get_selected_time_range (cal_view, &dtstart, &dtend)) {
        dtstart = now;
        dtend = dtstart + 3600;
    }

    if (no_past_date && dtstart < now) {
        dtend = time_day_begin (now) + (dtend - dtstart);
        dtstart = time_day_begin (now);
        do_rounding = TRUE;
    }

    /* We either need rounding or don't want to set all_day for this, we will rather use actual */
    /* time in this cases; dtstart should be a midnight in this case */
    if (do_rounding || (!all_day && (dtend - dtstart) == (60 * 60 * 24))) {
        struct tm local = *localtime (&now);
        gint time_div = e_calendar_view_get_time_divisions (cal_view);
        gint hours, mins;

        if (!time_div) /* Possible if your gconf values aren't so nice */
            time_div = 30;

        if (time_day_begin (now) == time_day_begin (dtstart)) {
            /* same day as today */
            hours = local.tm_hour;
            mins = local.tm_min;

            /* round minutes to nearest time division, up or down */
            if ((mins % time_div) >= time_div / 2)
                mins += time_div;
            mins = (mins - (mins % time_div));
        } else {
            /* other day than today */
            hours = e_cal_model_get_work_day_start_hour (model);
            mins = e_cal_model_get_work_day_start_minute (model);
        }

        dtstart = dtstart + (60 * 60 * hours) + (mins * 60);
        dtend = dtstart + (time_div * 60);
    }

    e_calendar_view_new_appointment_for (cal_view, dtstart, dtend, all_day, meeting);
}

void
e_calendar_view_new_appointment (ECalendarView *cal_view)
{
    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    e_calendar_view_new_appointment_full (cal_view, FALSE, FALSE, FALSE);
}

/* Ensures the calendar is selected */
static void
object_created_cb (CompEditor *ce, ECalendarView *cal_view)
{
    e_calendar_view_emit_user_created (cal_view);
}

CompEditor *
e_calendar_view_open_event_with_flags (ECalendarView *cal_view, ECal *client, icalcomponent *icalcomp, guint32 flags)
{
    CompEditor *ce;
    const gchar *uid;
    ECalComponent *comp;
    EShell *shell;

    /* FIXME ECalendarView should own an EShell pointer. */
    shell = e_shell_get_default ();

    uid = icalcomponent_get_uid (icalcomp);

    ce = comp_editor_find_instance (uid);
    if (!ce) {
        ce = event_editor_new (client, shell, flags);

        g_signal_connect (ce, "object_created", G_CALLBACK (object_created_cb), cal_view);

        comp = e_cal_component_new ();
        e_cal_component_set_icalcomponent (comp, icalcomponent_new_clone (icalcomp));
        comp_editor_edit_comp (ce, comp);
        if (flags & COMP_EDITOR_MEETING)
            event_editor_show_meeting (EVENT_EDITOR (ce));

        g_object_unref (comp);
    }

    gtk_window_present (GTK_WINDOW (ce));

    return ce;
}

/**
 * e_calendar_view_edit_appointment
 * @cal_view: A calendar view.
 * @client: Calendar client.
 * @icalcomp: The object to be edited.
 * @mode: one of #EEditEventMode
 *
 * Opens an editor window to allow the user to edit the selected
 * object.
 */
void
e_calendar_view_edit_appointment (ECalendarView *cal_view, ECal *client, icalcomponent *icalcomp, EEditEventMode mode)
{
    guint32 flags = 0;

    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));
    g_return_if_fail (E_IS_CAL (client));
    g_return_if_fail (icalcomp != NULL);

    if ((mode == EDIT_EVENT_AUTODETECT && icalcomponent_get_first_property (icalcomp, ICAL_ATTENDEE_PROPERTY) != NULL)
        || mode == EDIT_EVENT_FORCE_MEETING) {
        ECalComponent *comp = e_cal_component_new ();
        e_cal_component_set_icalcomponent (comp, icalcomponent_new_clone (icalcomp));
        flags |= COMP_EDITOR_MEETING;
        if (itip_organizer_is_user (comp, client) || itip_sentby_is_user (comp, client) || !e_cal_component_has_attendees (comp))
            flags |= COMP_EDITOR_USER_ORG;
        g_object_unref (comp);
    }

    e_calendar_view_open_event_with_flags (cal_view, client, icalcomp, flags);
}

void
e_calendar_view_modify_and_send (ECalComponent *comp,
                 ECal *client,
                 CalObjModType mod,
                 GtkWindow *toplevel,
                 gboolean new)
{
    gboolean only_new_attendees = FALSE;

    if (e_cal_modify_object (client, e_cal_component_get_icalcomponent (comp), mod, NULL)) {
        gboolean strip_alarms = TRUE;

        if ((itip_organizer_is_user (comp, client) || itip_sentby_is_user (comp, client)) &&
            send_component_dialog (toplevel, client, comp, new, &strip_alarms, &only_new_attendees)) {
            ECalComponent *send_comp = NULL;

            if (mod == CALOBJ_MOD_ALL && e_cal_component_is_instance (comp)) {
                /* Ensure we send the master object, not the instance only */
                icalcomponent *icalcomp = NULL;
                const gchar *uid = NULL;

                e_cal_component_get_uid (comp, &uid);
                if (e_cal_get_object (client, uid, NULL, &icalcomp, NULL) && icalcomp) {
                    send_comp = e_cal_component_new ();
                    if (!e_cal_component_set_icalcomponent (send_comp, icalcomp)) {
                        icalcomponent_free (icalcomp);
                        g_object_unref (send_comp);
                        send_comp = NULL;
                    } else if (only_new_attendees) {
                        /* copy new-attendees information too if required for later use */
                        comp_editor_copy_new_attendees (send_comp, comp);
                    }
                }
            }

            itip_send_comp (E_CAL_COMPONENT_METHOD_REQUEST, send_comp ? send_comp : comp, client, NULL, NULL, NULL, strip_alarms, only_new_attendees);

            if (send_comp)
                g_object_unref (send_comp);
        }
    } else {
        g_message (G_STRLOC ": Could not update the object!");
    }
}

static gboolean
tooltip_grab (GtkWidget *tooltip, GdkEventKey *event, ECalendarView *view)
{
    GtkWidget *widget = (GtkWidget *) g_object_get_data (G_OBJECT (view), "tooltip-window");

    if (!widget)
        return TRUE;

    gdk_keyboard_ungrab (GDK_CURRENT_TIME);
    gtk_widget_destroy (widget);
    g_object_set_data (G_OBJECT (view), "tooltip-window", NULL);

    return FALSE;
}

static gchar *
get_label (struct icaltimetype *tt, icaltimezone *f_zone, icaltimezone *t_zone)
{
    struct tm tmp_tm;

    tmp_tm = icaltimetype_to_tm_with_zone (tt, f_zone, t_zone);

    return e_datetime_format_format_tm ("calendar", "table", DTFormatKindDateTime, &tmp_tm);
}

void
e_calendar_view_move_tip (GtkWidget *widget, gint x, gint y)
{
    GtkAllocation allocation;
    GtkRequisition requisition;
    gint w, h;
    GdkScreen *screen;
    GdkScreen *pointer_screen;
    gint monitor_num, px, py;
    GdkRectangle monitor;

    screen = gtk_widget_get_screen (widget);

    gtk_widget_get_preferred_size (widget, &requisition, NULL);
    w = requisition.width;
    h = requisition.height;

    gdk_display_get_pointer (
        gdk_screen_get_display (screen),
        &pointer_screen, &px, &py, NULL);
    if (pointer_screen != screen) {
        px = x;
        py = y;
    }
    monitor_num = gdk_screen_get_monitor_at_point (screen, px, py);
    gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);

    if ((x + w) > monitor.x + monitor.width)
        x -= (x + w) - (monitor.x + monitor.width);
    else if (x < monitor.x)
        x = monitor.x;

    gtk_widget_get_allocation (widget, &allocation);

    if ((y + h + allocation.height + 4) > monitor.y + monitor.height)
        y = y - h - 36;

    gtk_window_move (GTK_WINDOW (widget), x, y);
    gtk_widget_show (widget);
}

/**
 * Returns information about attendees in the component. If no attendees, then returns NULL.
 * The information is like "Status: Accepted: X   Declined: Y  ...".
 * Free returned pointer with g_free.
 **/
gchar *
e_calendar_view_get_attendees_status_info (ECalComponent *comp, ECal *client)
{
    struct _values {
        icalparameter_partstat status;
        const gchar *caption;
        gint count;
    } values[] = {
        { ICAL_PARTSTAT_ACCEPTED,    N_("Accepted"),     0 },
        { ICAL_PARTSTAT_DECLINED,    N_("Declined"),     0 },
        { ICAL_PARTSTAT_TENTATIVE,   N_("Tentative"),    0 },
        { ICAL_PARTSTAT_DELEGATED,   N_("Delegated"),    0 },
        { ICAL_PARTSTAT_NEEDSACTION, N_("Needs action"), 0 },
        { ICAL_PARTSTAT_NONE,        N_("Other"),        0 },
        { ICAL_PARTSTAT_X,           NULL,              -1 }
    };

    GSList *attendees = NULL, *a;
    gboolean have = FALSE;
    gchar *res = NULL;
    gint i;

    if (!comp || !e_cal_component_has_attendees (comp) || !itip_organizer_is_user_ex (comp, client, TRUE))
        return NULL;

    e_cal_component_get_attendee_list (comp, &attendees);

    for (a = attendees; a; a = a->next) {
        ECalComponentAttendee *att = a->data;

        if (att && att->cutype == ICAL_CUTYPE_INDIVIDUAL &&
            (att->role == ICAL_ROLE_CHAIR ||
             att->role == ICAL_ROLE_REQPARTICIPANT ||
             att->role == ICAL_ROLE_OPTPARTICIPANT)) {
            have = TRUE;

            for (i = 0; values[i].count != -1; i++) {
                if (att->status == values[i].status || values[i].status == ICAL_PARTSTAT_NONE) {
                    values[i].count++;
                    break;
                }
            }
        }
    }

    if (have) {
        GString *str = g_string_new ("");

        for (i = 0; values[i].count != -1; i++) {
            if (values[i].count > 0) {
                if (str->str && *str->str)
                    g_string_append (str, "   ");

                g_string_append_printf (str, "%s: %d", _(values[i].caption), values[i].count);
            }
        }

        g_string_prepend (str, ": ");

        /* To Translators: 'Status' here means the state of the attendees, the resulting string will be in a form:
           Status: Accepted: X   Declined: Y   ... */
        g_string_prepend (str, _("Status"));

        res = g_string_free (str, FALSE);
    }

    if (attendees)
        e_cal_component_free_attendee_list (attendees);

    return res;
}

/*
 * It is expected to show the tooltips in this below format
 *
 *  <B>SUBJECT OF THE MEETING</B>
 *  Organiser: NameOfTheUser<email@ofuser.com>
 *  Location: PlaceOfTheMeeting
 *  Time : DateAndTime (xx Minutes)
 *      Status: Accepted: X   Declined: Y   ...
 */

gboolean
e_calendar_view_get_tooltips (const ECalendarViewEventData *data)
{
    GtkWidget *label, *box, *hbox, *ebox, *frame;
    const gchar *str;
    gchar *tmp, *tmp1, *tmp2;
    ECalComponentOrganizer organiser;
    ECalComponentDateTime dtstart, dtend;
    icalcomponent *clone_comp;
    time_t t_start, t_end;
    ECalendarViewEvent *pevent;
    GtkStyle *style = gtk_widget_get_default_style ();
    GtkWidget *widget = (GtkWidget *) g_object_get_data (G_OBJECT (data->cal_view), "tooltip-window");
    GdkWindow *window;
    ECalComponent *newcomp = e_cal_component_new ();
    icaltimezone *zone, *default_zone;
    ECal *client = NULL;
    gboolean free_text = FALSE;

    /* Delete any stray tooltip if left */
    if (widget)
        gtk_widget_destroy (widget);

    default_zone = e_calendar_view_get_timezone  (data->cal_view);
    pevent = data->get_view_event (data->cal_view, data->day, data->event_num);

    if (!is_comp_data_valid (pevent))
        return FALSE;

    client = pevent->comp_data->client;

    clone_comp = icalcomponent_new_clone (pevent->comp_data->icalcomp);
    if (!e_cal_component_set_icalcomponent (newcomp, clone_comp))
        g_warning ("couldn't update calendar component with modified data from backend\n");

    box = gtk_vbox_new (FALSE, 0);

    str = e_calendar_view_get_icalcomponent_summary (pevent->comp_data->client, pevent->comp_data->icalcomp, &free_text);

    if (!(str && *str)) {
        g_object_unref (newcomp);
        gtk_widget_destroy (box);

        return FALSE;
    }

    tmp = g_markup_printf_escaped ("<b>%s</b>", str);
    label = gtk_label_new (NULL);
    gtk_label_set_line_wrap ((GtkLabel *)label, TRUE);
    gtk_label_set_markup ((GtkLabel *)label, tmp);

    if (free_text) {
        g_free ((gchar *)str);
        str = NULL;
    }

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_box_pack_start ((GtkBox *)hbox, label, FALSE, FALSE, 0);
    ebox = gtk_event_box_new ();
    gtk_container_add ((GtkContainer *)ebox, hbox);
    gtk_widget_modify_bg (ebox, GTK_STATE_NORMAL, &(style->bg[GTK_STATE_SELECTED]));
    gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &(style->text[GTK_STATE_SELECTED]));

    gtk_box_pack_start ((GtkBox *)box, ebox, FALSE, FALSE, 0);
    g_free (tmp);

    e_cal_component_get_organizer (newcomp, &organiser);
    if (organiser.cn) {
        gchar *ptr;
        ptr = strchr (organiser.value, ':');

        if (ptr) {
            ptr++;
            /* To Translators: It will display "Organiser: NameOfTheUser <email@ofuser.com>" */
            tmp = g_strdup_printf (_("Organizer: %s <%s>"), organiser.cn, ptr);
        }
        else
            /* With SunOne accouts, there may be no ':' in organiser.value*/
            tmp = g_strdup_printf (_("Organizer: %s"), organiser.cn);

        label = gtk_label_new (tmp);
        hbox = gtk_hbox_new (FALSE, 0);
        gtk_box_pack_start ((GtkBox *)hbox, label, FALSE, FALSE, 0);
        ebox = gtk_event_box_new ();
        gtk_container_add ((GtkContainer *)ebox, hbox);
        gtk_box_pack_start ((GtkBox *)box, ebox, FALSE, FALSE, 0);

        g_free (tmp);
    }

    e_cal_component_get_location (newcomp, &str);

    if (str) {
        /* To Translators: It will display "Location: PlaceOfTheMeeting" */
        tmp = g_markup_printf_escaped (_("Location: %s"), str);
        label = gtk_label_new (NULL);
        gtk_label_set_markup ((GtkLabel *)label, tmp);
        hbox = gtk_hbox_new (FALSE, 0);
        gtk_box_pack_start ((GtkBox *)hbox, label, FALSE, FALSE, 0);
        ebox = gtk_event_box_new ();
        gtk_container_add ((GtkContainer *)ebox, hbox);
        gtk_box_pack_start ((GtkBox *)box, ebox, FALSE, FALSE, 0);
        g_free (tmp);
    }
    e_cal_component_get_dtstart (newcomp, &dtstart);
    e_cal_component_get_dtend (newcomp, &dtend);

    if (dtstart.tzid) {
        zone = icalcomponent_get_timezone (e_cal_component_get_icalcomponent (newcomp), dtstart.tzid);
        if (!zone)
            e_cal_get_timezone (client, dtstart.tzid, &zone, NULL);

        if (!zone)
            zone = default_zone;

    } else {
        zone = NULL;
    }
    t_start = icaltime_as_timet_with_zone (*dtstart.value, zone);
    t_end = icaltime_as_timet_with_zone (*dtend.value, zone);

    tmp1 = get_label (dtstart.value, zone, default_zone);
    tmp = calculate_time (t_start, t_end);

    /* To Translators: It will display "Time: ActualStartDateAndTime (DurationOfTheMeeting)"*/
    tmp2 = g_strdup_printf(_("Time: %s %s"), tmp1, tmp);
    if (zone && !cal_comp_util_compare_event_timezones (newcomp, client, default_zone)) {
        g_free (tmp);
        g_free (tmp1);

        tmp1 = get_label (dtstart.value, zone, zone);
        tmp = g_strconcat (tmp2, "\n\t[ ", tmp1, " ", icaltimezone_get_display_name (zone), " ]", NULL);
    } else {
        g_free (tmp);
        tmp = tmp2;
        tmp2 = NULL;
    }

    e_cal_component_free_datetime (&dtstart);
    e_cal_component_free_datetime (&dtend);

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_box_pack_start ((GtkBox *)hbox, gtk_label_new_with_mnemonic (tmp), FALSE, FALSE, 0);
    ebox = gtk_event_box_new ();
    gtk_container_add ((GtkContainer *)ebox, hbox);
    gtk_box_pack_start ((GtkBox *)box, ebox, FALSE, FALSE, 0);

    g_free (tmp);
    g_free (tmp2);
    g_free (tmp1);

    tmp = e_calendar_view_get_attendees_status_info (newcomp, pevent->comp_data->client);
    if (tmp) {
        hbox = gtk_hbox_new (FALSE, 0);
        gtk_box_pack_start ((GtkBox *)hbox, gtk_label_new (tmp), FALSE, FALSE, 0);
        ebox = gtk_event_box_new ();
        gtk_container_add ((GtkContainer *)ebox, hbox);
        gtk_box_pack_start ((GtkBox *)box, ebox, FALSE, FALSE, 0);

        g_free (tmp);
    }

    pevent->tooltip = gtk_window_new (GTK_WINDOW_POPUP);
    frame = gtk_frame_new (NULL);
    gtk_frame_set_shadow_type ((GtkFrame *)frame, GTK_SHADOW_IN);

    gtk_window_set_type_hint (GTK_WINDOW (pevent->tooltip), GDK_WINDOW_TYPE_HINT_TOOLTIP);
    gtk_window_move ((GtkWindow *)pevent->tooltip, pevent->x +16, pevent->y+16);
    gtk_container_add ((GtkContainer *)frame, box);
    gtk_container_add ((GtkContainer *)pevent->tooltip, frame);

    gtk_widget_show_all (pevent->tooltip);

    e_calendar_view_move_tip (pevent->tooltip, pevent->x +16, pevent->y+16);

    window = gtk_widget_get_window (pevent->tooltip);
    gdk_keyboard_grab (window, FALSE, GDK_CURRENT_TIME);
    g_signal_connect (pevent->tooltip, "key-press-event", G_CALLBACK (tooltip_grab), data->cal_view);
    pevent->timeout = -1;

    g_object_set_data (G_OBJECT (data->cal_view), "tooltip-window", pevent->tooltip);
    g_object_unref (newcomp);

    return FALSE;
}

static gboolean
icalcomp_contains_category (icalcomponent *icalcomp, const gchar *category)
{
    icalproperty *property;

    g_return_val_if_fail (icalcomp != NULL && category != NULL, FALSE);

    for (property = icalcomponent_get_first_property (icalcomp, ICAL_CATEGORIES_PROPERTY);
         property != NULL;
         property = icalcomponent_get_next_property (icalcomp, ICAL_CATEGORIES_PROPERTY)) {
        gchar *value = icalproperty_get_value_as_string_r (property);

        if (value && strcmp (category, value) == 0) {
            g_free (value);
            return TRUE;
        }
        g_free (value);
    }

    return FALSE;
}

/* e_calendar_view_get_icalcomponent_summary returns summary of calcomp,
 * and for type of birthday or anniversary it append number of years since
 * beginning. In this case, the free_text is set to TRUE and caller need
 * to g_free returned string, otherwise free_text is set to FALSE and
 * returned value is owned by calcomp.
 */

const gchar *
e_calendar_view_get_icalcomponent_summary (ECal *ecal, icalcomponent *icalcomp, gboolean *free_text)
{
    const gchar *summary;

    g_return_val_if_fail (icalcomp != NULL && free_text != NULL, NULL);

    *free_text = FALSE;
    summary = icalcomponent_get_summary (icalcomp);

    if (icalcomp_contains_category (icalcomp, _("Birthday")) ||
        icalcomp_contains_category (icalcomp, _("Anniversary"))) {
        struct icaltimetype dtstart, dtnow;
        icalcomponent *item_icalcomp = NULL;

        if (e_cal_get_object (ecal,
                      icalcomponent_get_uid (icalcomp),
                      icalcomponent_get_relcalid (icalcomp),
                      &item_icalcomp,
                      NULL)) {
            dtstart = icalcomponent_get_dtstart (item_icalcomp);
            dtnow = icalcomponent_get_dtstart (icalcomp);

            if (dtnow.year - dtstart.year > 0) {
                summary = g_strdup_printf ("%s (%d)", summary ? summary : "", dtnow.year - dtstart.year);
                *free_text = summary != NULL;
            }
        }
    }

    return summary;
}

void
e_calendar_view_emit_user_created (ECalendarView *cal_view)
{
    g_return_if_fail (E_IS_CALENDAR_VIEW (cal_view));

    g_signal_emit (cal_view, signals[USER_CREATED], 0);
}

void
draw_curved_rectangle (cairo_t *cr, double x0, double y0,
            gdouble rect_width, double rect_height,
            gdouble radius)
{
    gdouble x1, y1;

    x1 = x0 + rect_width;
    y1 = y0 + rect_height;

    if (!rect_width || !rect_height)
        return;
    if (rect_width / 2 < radius) {
        if (rect_height / 2 < radius) {
        cairo_move_to  (cr, x0, (y0 + y1)/2);
        cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
        cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
        cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
        cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
        } else {
        cairo_move_to  (cr, x0, y0 + radius);
        cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
        cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
        cairo_line_to (cr, x1 , y1 - radius);
        cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
        cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
        }
    } else {
        if (rect_height / 2 < radius) {
        cairo_move_to  (cr, x0, (y0 + y1)/2);
        cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
        cairo_line_to (cr, x1 - radius, y0);
        cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
        cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
        cairo_line_to (cr, x0 + radius, y1);
        cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
        } else {
        cairo_move_to  (cr, x0, y0 + radius);
        cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
        cairo_line_to (cr, x1 - radius, y0);
        cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
        cairo_line_to (cr, x1 , y1 - radius);
        cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
        cairo_line_to (cr, x0 + radius, y1);
        cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
        }
    }
    cairo_close_path (cr);
}

/* returns either light or dark yellow, based on the base_background,
   which is the default background color */
GdkColor
get_today_background (const GdkColor base_background)
{
    GdkColor res = base_background;

    if (res.red > 0x7FFF) {
        /* light yellow for a light theme */
        res.red   = 0xFFFF;
        res.green = 0xFFFF;
        res.blue  = 0xC0C0;
    } else {
        /* dark yellow for a dark theme */
        res.red   = 0x3F3F;
        res.green = 0x3F3F;
        res.blue  = 0x0000;
    }

    return res;
}

gboolean
is_comp_data_valid_func (ECalendarViewEvent *event, const gchar *location)
{
    g_return_val_if_fail (location != NULL, FALSE);

    if (!event) {
        g_warning ("%s: event is NULL", location);
        return FALSE;
    }

    if (!event->comp_data) {
        g_warning ("%s: event's (%p) comp_data is NULL", location, event);
        return FALSE;
    }

    return TRUE;
}

gboolean
is_array_index_in_bounds_func (GArray *array, gint index, const gchar *location)
{
    g_return_val_if_fail (location != NULL, FALSE);

    if (!array) {
        g_warning ("%s: array is NULL", location);
        return FALSE;
    }

    if (index < 0 || index >= array->len) {
        g_warning ("%s: index %d is out of bounds [0,%d) at array %p", location, index, array->len, array);
        return FALSE;
    }

    return TRUE;
}