HEAD: rearrange things a bit so we can have a libwvbase.so, which contains
[wvapps.git] / evolution / evoutils.cc
blob41ee5cc5ee8440829fc540458651004f65a8676a
1 #include "evoutils.h"
3 #include "wvstringlist.h"
4 #include "wvlog.h"
6 #include <sys/types.h>
7 #include <dirent.h>
8 #include <unistd.h>
9 #include <sys/stat.h>
10 #include <errno.h>
12 #include <glib.h>
13 #include <cassert>
15 void get_evo_uri_from_eit_key(WvStringParm eit_key, WvStringParm storage_owner, WvString &evo_uri)
17 WvStringList eit_folder_keys;
18 eit_folder_keys.split(eit_key, ".");
19 evo_uri = "";
21 if (eit_folder_keys.count() < 2)
22 return;
24 WvString owner = eit_folder_keys.popstr();
25 if (strcmp(storage_owner.cstr(), owner.cstr()) != 0)
26 evo_uri.append("/Public Folders/%s", owner);
28 WvStringList::Iter folder_key(eit_folder_keys);
29 folder_key.rewind();
31 for(;folder_key.next();)
33 evo_uri.append("/%s", folder_key());
38 void get_physical_path_from_evo_uri(WvStringParm evo_uri, WvString &path, WvString &subfolders_path)
40 path = "";
41 subfolders_path = "";
43 path.append("%s/evolution/local/", g_get_home_dir());
44 WvStringList dirs;
46 dirs.split((evo_uri.cstr()+1), "/");
47 if (dirs.count() < 2)
48 return;
50 path.append(dirs.popstr());
51 WvStringList::Iter dir(dirs);
52 dir.rewind();
54 for(;dir.next();)
56 subfolders_path = path;
57 subfolders_path.append("/subfolders");
58 path.append("/subfolders/%s", dir());
62 bool eitpath_is_local(WvStringParm path, WvStringParm owner)
64 return !strncmp(path, owner, owner.len());
67 WvString convert_eitpath_to_evopath(WvStringParm eitpath, WvStringParm owner)
69 WvLog log("convert_eitpath_to_evopath");
70 WvStringList dirs;
71 dirs.split(eitpath, ".");
73 WvString evopath;
74 WvStringList::Iter i(dirs);
75 //assert(dirs.count() > 1);
77 if (dirs.count() <= 1)
79 evopath = "";
80 return evopath;
83 i.rewind();
84 i.next();
85 if (owner != i())
86 evopath.append("/Public Folders/%s", i());
88 while (i.next()) // skip folder owner
90 evopath.append("/");
91 evopath.append(i());
94 return evopath;
97 WvString convert_evopath_to_eitpath(WvStringParm evopath, WvStringParm owner)
99 // FIXME: What to do with folder names with '.' in them?
100 WvStringList dirs;
101 dirs.split(evopath, "/");
103 WvString eitpath("");
104 WvStringList::Iter i(dirs);
105 i.rewind();
106 do {
107 i.next();
108 } while (!i());
109 if (i() == "Public Folders")
111 if (i.next())
112 eitpath = i();
114 else
115 eitpath.append("%s.%s", owner, i());
117 while (i.next())
119 if (!!i())
121 eitpath.append(".");
122 eitpath.append(i());
126 return eitpath;
129 void destroy_dir(WvStringParm path)
131 // FIXME: We might actually want to do some error checking here and warn
132 // the user if things are going badly here. These operations should never
133 // fail normally though.. and if they do, the worst that can happen is that
134 // the user won't be able to create a folder.. whopdee doo
136 DIR *dir = opendir(path.cstr());
137 if(dir == NULL) // nothing to do, can't access dir (probably 'cause it's already gone)
138 return;
140 struct dirent *current = readdir(dir);
141 while(current)
143 // The following 10 lines of code are from the friendly hackers @ Ximian
144 // Ignore . and ..
145 if(current->d_name[0] == '.')
147 if(current->d_name[1] == '\0' ||
148 (current->d_name[1] == '.' && current->d_name[2] == '\0'))
150 current = readdir(dir);
151 continue;
155 WvString full_path("%s/%s", path, current->d_name);
156 struct stat buf;
157 stat(full_path.cstr(), &buf);
158 unlink(full_path.cstr());
159 current = readdir(dir);
163 rmdir(path.cstr()); // this will vonly succeed when it should