1 #include "exchangeitstorage.h"
2 #include "localstorage.h"
3 #include "calendaradaptor.h"
4 #include "contactsadaptor.h"
10 #include <sys/types.h>
12 // write_data_to_file: writes a string to a file
13 // returns 1 if successful, 0 otherwise
14 int write_data_to_file(WvStringParm path
, WvStringParm file_name
,
18 WvString
uri("%s/%s", path
, file_name
);
19 if (unlink(uri
.cstr()) != 0 && (errno
!= ENOENT
))
21 g_message("write_data_to_file: Couldn't unlink file %s\n", uri
.cstr());
25 WvFile
file(uri
, (O_WRONLY
|O_CREAT
), 0600);
28 g_message("write_data_to_file: Couldn't create file %s for writing\n",
33 file
.write(data
, data
.len()); // FIXME: How do we check for errors here?
34 // just checking for num_written seems to
35 // give incorrect results
37 size_t num_written
= 0;
38 if (num_written
!= file
.write(data
, data
.len()))
40 g_message("write_data_to_file: Failed to actually write data for file %s\n",
42 g_message("write_data_to_file: Expected to write: %i. Wrote: %i\n",
43 data
.len(), num_written
);
50 int write_metadata_to_file(WvStringParm path
, WvStringParm type
,
51 WvStringParm desc
, WvStringParm icon
)
53 //WvString folder_metadata(folder_metadata_template, type, desc);
54 //return write_data_to_file(path, "folder-metadata.xml", folder_metadata);
55 WvString
folder_metadata("%s\n%s\n%s\n", type
, desc
, icon
);
56 return write_data_to_file(path
, ".meta", folder_metadata
);
59 ExchangeItStorage::ExchangeItStorage(WvStringParm _base_path
,
62 base_path(_base_path
),
63 log("ExchangeItStorage"),
70 WvString
state_path("%s/evolution-exchangeit.ini", g_get_home_dir());
71 state
.mountgen(new UniIniGen(state_path
));
75 ExchangeItStorage::~ExchangeItStorage()
80 void ExchangeItStorage::_create_folder_hierarchy(WvString
&path
, WvStringList
&subfolders
)
82 while (!subfolders
.isempty())
84 path
.append("/%s", subfolders
.popstr());
87 if (!subfolders
.isempty())
88 write_metadata_to_file(path
, "", "", "folder");
92 void ExchangeItStorage::add_adaptor(WvStringParm key
, WvStringParm type
, const bool local
)
94 log("Adding adaptor\n");
96 // Carve out some space in the local store for our entry
97 WvStringList subfolders
;
98 WvString path
= base_path
;
99 subfolders
.split(key
, ".");
100 assert(subfolders
.count() >= 2);
104 subfolders
.popstr(); // username, which we ignore in the local case
105 path
.append("/%s", subfolders
.popstr());
107 if (!subfolders
.isempty())
108 _create_folder_hierarchy(path
, subfolders
);
112 path
.append("/Public Folders");
114 if (!write_metadata_to_file(path
, "", "", "public-folder"))
116 log("Couldn't write metadata to file!\n");
119 assert(!subfolders
.isempty());
120 _create_folder_hierarchy(path
, subfolders
);
123 ExchangeItAdaptor
*adaptor
= NULL
;
125 if(type
== CALENDAR_TYPE
)
127 adaptor
= new CalendarAdaptor(key
, path
, state
);
129 else if (type
== CONTACTS_TYPE
)
131 adaptor
= new ContactsAdaptor(key
, path
, state
);
136 dict
.add(adaptor
, true);
141 void ExchangeItStorage::remove_adaptor(WvStringParm key
)
143 ExchangeItAdaptor
*adaptor
= dict
[key
];
144 dict
.remove(adaptor
);