aboutsummaryrefslogtreecommitdiffstats
path: root/tools/evolution-move-tasks
blob: 05c27cbce20e5401373860087cb74b92e35ce878 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env perl
# The above is a portable way to invoke Perl, according to the GNU Autotools
# book. It is useful since we don't know where perl is installed.
#
# evolution-move-tasks: a Perl script to move tasks from the Calendar folder
#   to the new Tasks folder.
#

use diagnostics;

# You may have to change this if your Evolution files are somewhere else.
$EVOLUTION_DIR = "$ENV{'HOME'}/evolution";

$CALENDAR_DIR = "$EVOLUTION_DIR/local/Calendar";
$TASKS_DIR = "$EVOLUTION_DIR/local/Tasks";

# Create the Tasks folder if needed.
&EnsureTasksFolderExists ($TASKS_DIR);

# Get any tasks from the calendar .ics file.
$tasks = &LoadTasks ("$CALENDAR_DIR/calendar.ics");

# Get any tasks already in the tasks .ics file.
$tasks .= &LoadTasks ("$TASKS_DIR/tasks.ics");

# Create a new Tasks .ics file containing all the tasks.
&OutputTasks ("$TASKS_DIR/tasks.new", $tasks);

# Move the existing tasks file to a backup.
if (-e "$TASKS_DIR/tasks.ics") {
    rename "$TASKS_DIR/tasks.ics", "$TASKS_DIR/tasks.bak"
    || die "Can't rename $TASKS_DIR/tasks.ics to $TASKS_DIR/tasks.bak";
}

# Move the new file into position.
rename "$TASKS_DIR/tasks.new", "$TASKS_DIR/tasks.ics"
    || die "Can't rename $TASKS_DIR/tasks.new to $TASKS_DIR/tasks.ics";

# Move the new Calendar file (without the Tasks) into position.
rename "$CALENDAR_DIR/calendar.ics.new", "$CALENDAR_DIR/calendar.ics"
    || die "Can't rename $TASKS_DIR/tasks.new to $TASKS_DIR/tasks.ics";

0;


# If the evolution/local/Tasks folder does not exist, this creates it and
# creates the metadata XML file.
sub EnsureTasksFolderExists {
    my ($tasks_dir) = @_;

    return if (-e $tasks_dir);

    print "Creating Tasks folder in: $tasks_dir\n";

    mkdir ($tasks_dir, 0777)
    || die "Can't create Tasks folder directory: $tasks_dir";

    $metadata = "$tasks_dir/folder-metadata.xml";
    open (METADATA, ">$metadata")
    || die "Can't create metadata file: $metadata";

    print METADATA <<EOF;
<?xml version="1.0"?>
<efolder>
    <type>tasks</type>
    <description>Tasks</description>
</efolder>
EOF

    close (METADATA);
}


sub LoadTasks {
    my ($icalendar_file) = @_;

    return "" if (! -e $icalendar_file);

    open (ICSFILE, $icalendar_file)
    || die "Can't open iCalendar file: $icalendar_file";

    open (NEWICSFILE, ">$icalendar_file.new")
    || die "Can't open iCalendar file: $icalendar_file.new";

    $tasks = "";
    $in_task = 0;
    while (<ICSFILE>) {
    if ($in_task) {
        $tasks .= $_;

        if (m/^END:VTODO/) {
        $in_task = 0;
        }

    } else {
        if (m/^BEGIN:VTODO/) {
        print "Found task\n";

        $tasks .= $_;
        $in_task = 1;
        } else {
        print NEWICSFILE $_;
        }
    }
    }

    close (NEWICSFILE);
    close (ICSFILE);

    return $tasks;
}


sub OutputTasks {
    my ($icalendar_file, $tasks) = @_;

    open (ICSFILE, ">$icalendar_file")
    || die "Can't create iCalendar file: $icalendar_file";

    print ICSFILE <<EOF;
BEGIN:VCALENDAR
CALSCALE
 :GREGORIAN
PRODID
 :-//Helix Code//NONSGML Evolution Calendar//EN
VERSION
 :2.0
$tasks
END:VCALENDAR
EOF

    close (ICSFILE);
}