schedulator: added a "result" plugin that records the finished schedule in a database...
[wvapps.git] / evolution / evoutils.cc
blob9d4f79d354de20160f589e9afa14d676d504da31
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: s; c-basic-offset: 4 -*- */
2 /*
3 * Authors : Patrick Patterson <ppatters@nit.ca>
4 * Scott MacLean <scott@nit.ca>
5 * William Lachance <wlach@nit.ca>
7 * Copyright 2003-2004, Net Integration Technologies, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
24 #include "evoutils.h"
26 #include "wvstringlist.h"
27 #include "wvlog.h"
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <errno.h>
35 #include <glib.h>
36 #include <cassert>
38 void get_evo_uri_from_eit_key(WvStringParm eit_key, WvStringParm storage_owner, WvString &evo_uri)
40 WvStringList eit_folder_keys;
41 eit_folder_keys.split(eit_key, ".");
42 evo_uri = "";
44 if (eit_folder_keys.count() < 2)
45 return;
47 WvString owner = eit_folder_keys.popstr();
48 if (strcmp(storage_owner.cstr(), owner.cstr()) != 0)
49 evo_uri.append("/Public Folders/%s", owner);
51 WvStringList::Iter folder_key(eit_folder_keys);
52 folder_key.rewind();
54 for(;folder_key.next();)
56 evo_uri.append("/%s", folder_key());
61 void get_physical_path_from_evo_uri(WvStringParm evo_uri, WvString &path, WvString &subfolders_path)
63 path = "";
64 subfolders_path = "";
66 path.append("%s/evolution/local/", g_get_home_dir());
67 WvStringList dirs;
69 dirs.split((evo_uri.cstr()+1), "/");
70 if (dirs.count() < 2)
71 return;
73 path.append(dirs.popstr());
74 WvStringList::Iter dir(dirs);
75 dir.rewind();
77 for(;dir.next();)
79 subfolders_path = path;
80 subfolders_path.append("/subfolders");
81 path.append("/subfolders/%s", dir());
85 bool eitpath_is_local(WvStringParm path, WvStringParm owner)
87 return !strncmp(path, owner, owner.len());
90 WvString convert_eitpath_to_evopath(WvStringParm eitpath, WvStringParm owner)
92 WvLog log("convert_eitpath_to_evopath");
93 WvStringList dirs;
94 dirs.split(eitpath, ".");
96 WvString evopath;
97 WvStringList::Iter i(dirs);
98 //assert(dirs.count() > 1);
100 if (dirs.count() <= 1)
102 evopath = "";
103 return evopath;
106 i.rewind();
107 i.next();
108 if (owner != i())
109 evopath.append("/Public Folders/%s", i());
111 while (i.next()) // skip folder owner
113 evopath.append("/");
114 evopath.append(i());
117 return evopath;
120 WvString convert_evopath_to_eitpath(WvStringParm evopath, WvStringParm owner)
122 // FIXME: What to do with folder names with '.' in them?
123 WvStringList dirs;
124 dirs.split(evopath, "/");
126 WvString eitpath("");
127 WvStringList::Iter i(dirs);
128 i.rewind();
129 do {
130 i.next();
131 } while (!i());
132 if (i() == "Public Folders")
134 if (i.next())
135 eitpath = i();
137 else
138 eitpath.append("%s.%s", owner, i());
140 while (i.next())
142 if (!!i())
144 eitpath.append(".");
145 eitpath.append(i());
149 return eitpath;
152 void destroy_dir(WvStringParm path)
154 // FIXME: We might actually want to do some error checking here and warn
155 // the user if things are going badly here. These operations should never
156 // fail normally though.. and if they do, the worst that can happen is that
157 // the user won't be able to create a folder.. whopdee doo
159 DIR *dir = opendir(path.cstr());
160 if(dir == NULL) // nothing to do, can't access dir (probably 'cause it's already gone)
161 return;
163 struct dirent *current = readdir(dir);
164 while(current)
166 // The following 10 lines of code are from the friendly hackers @ Ximian
167 // Ignore . and ..
168 if(current->d_name[0] == '.')
170 if(current->d_name[1] == '\0' ||
171 (current->d_name[1] == '.' && current->d_name[2] == '\0'))
173 current = readdir(dir);
174 continue;
178 WvString full_path("%s/%s", path, current->d_name);
179 struct stat buf;
180 stat(full_path.cstr(), &buf);
181 unlink(full_path.cstr());
182 current = readdir(dir);
186 rmdir(path.cstr()); // this will vonly succeed when it should