Refactor ELocalStorage a bit so that it's easier to implement the

missing operations.

svn path=/trunk/; revision=8913
This commit is contained in:
Ettore Perazzoli
2001-03-23 05:07:40 +00:00
parent 35c7cc9018
commit e8d6fc2756
2 changed files with 238 additions and 224 deletions

View File

@ -1,3 +1,18 @@
2001-03-23 Ettore Perazzoli <ettore@ximian.com>
* e-local-storage.c (impl_async_xfer_folder): New, implementation
for `EStorage::async_xfer_folder'.
(class_init): Install it.
(check_valid_name): Removed. Conflicts shouldn't be handled this
way, and hardcoding the names is *bad*.
(create_folder_directory): New helper function.
(real_do_folder_create): Removed.
(create_folder): New. Do things here using
`create_folder_directory'.
(impl_async_create_folder): Use `create_folder'.
(notify_bonobo_listener): Renamed from `notify_listener'. Moved
on top for clarity.
2001-03-22 Iain Holmes <iain@ximian.com>
* importer/evolution-importer-listener.c (evolution_importer_listener_new):

View File

@ -69,6 +69,65 @@ struct _ELocalStoragePrivate {
EvolutionLocalStorage *bonobo_interface;
};
/* EStorageResult <-> errno mapping. */
static EStorageResult
errno_to_storage_result (void)
{
EStorageResult storage_result;
switch (errno) {
case EACCES:
case EROFS:
storage_result = E_STORAGE_PERMISSIONDENIED;
break;
case EEXIST:
storage_result = E_STORAGE_EXISTS;
break;
case ENOSPC:
storage_result = E_STORAGE_NOSPACE;
break;
default:
storage_result = E_STORAGE_GENERICERROR;
}
return storage_result;
}
static EStorageResult
shell_component_result_to_storage_result (EvolutionShellComponentResult result)
{
/* FIXME: Maybe we need better mapping here. */
switch (result) {
case EVOLUTION_SHELL_COMPONENT_OK:
return E_STORAGE_OK;
case EVOLUTION_SHELL_COMPONENT_NOTFOUND:
return E_STORAGE_NOTFOUND;
case EVOLUTION_SHELL_COMPONENT_UNSUPPORTEDTYPE:
return E_STORAGE_UNSUPPORTEDTYPE;
case EVOLUTION_SHELL_COMPONENT_UNSUPPORTEDOPERATION:
return E_STORAGE_UNSUPPORTEDOPERATION;
case EVOLUTION_SHELL_COMPONENT_EXISTS:
return E_STORAGE_EXISTS;
case EVOLUTION_SHELL_COMPONENT_PERMISSIONDENIED:
return E_STORAGE_PERMISSIONDENIED;
case EVOLUTION_SHELL_COMPONENT_ALREADYOWNED:
case EVOLUTION_SHELL_COMPONENT_BUSY:
case EVOLUTION_SHELL_COMPONENT_CORBAERROR:
case EVOLUTION_SHELL_COMPONENT_HASSUBFOLDERS:
case EVOLUTION_SHELL_COMPONENT_INTERNALERROR:
case EVOLUTION_SHELL_COMPONENT_INTERRUPTED:
case EVOLUTION_SHELL_COMPONENT_INVALIDARG:
case EVOLUTION_SHELL_COMPONENT_INVALIDURI:
case EVOLUTION_SHELL_COMPONENT_NOSPACE:
case EVOLUTION_SHELL_COMPONENT_NOTOWNED:
case EVOLUTION_SHELL_COMPONENT_UNKNOWNERROR:
default:
return E_STORAGE_GENERICERROR;
}
}
/* Utility functions. */
@ -261,60 +320,24 @@ load_all_folders (ELocalStorage *local_storage)
return load_folders (local_storage, NULL, G_DIR_SEPARATOR_S, base_path);
}
static EStorageResult
errno_to_storage_result (void)
static void
notify_bonobo_listener (const Bonobo_Listener listener,
EStorageResult result,
const char *physical_path)
{
EStorageResult storage_result;
CORBA_any any;
GNOME_Evolution_Storage_FolderResult folder_result;
CORBA_Environment ev;
switch (errno) {
case EACCES:
case EROFS:
storage_result = E_STORAGE_PERMISSIONDENIED;
break;
case EEXIST:
storage_result = E_STORAGE_EXISTS;
break;
case ENOSPC:
storage_result = E_STORAGE_NOSPACE;
break;
default:
storage_result = E_STORAGE_GENERICERROR;
}
folder_result.result = result;
folder_result.path = CORBA_string_dup (physical_path ? physical_path : "");
any._type = TC_GNOME_Evolution_Storage_FolderResult;
any._value = &folder_result;
return storage_result;
}
static EStorageResult
shell_component_result_to_storage_result (EvolutionShellComponentResult result)
{
/* FIXME: Maybe we need better mapping here. */
switch (result) {
case EVOLUTION_SHELL_COMPONENT_OK:
return E_STORAGE_OK;
case EVOLUTION_SHELL_COMPONENT_NOTFOUND:
return E_STORAGE_NOTFOUND;
case EVOLUTION_SHELL_COMPONENT_UNSUPPORTEDTYPE:
return E_STORAGE_UNSUPPORTEDTYPE;
case EVOLUTION_SHELL_COMPONENT_UNSUPPORTEDOPERATION:
return E_STORAGE_UNSUPPORTEDOPERATION;
case EVOLUTION_SHELL_COMPONENT_EXISTS:
return E_STORAGE_EXISTS;
case EVOLUTION_SHELL_COMPONENT_PERMISSIONDENIED:
return E_STORAGE_PERMISSIONDENIED;
case EVOLUTION_SHELL_COMPONENT_ALREADYOWNED:
case EVOLUTION_SHELL_COMPONENT_BUSY:
case EVOLUTION_SHELL_COMPONENT_CORBAERROR:
case EVOLUTION_SHELL_COMPONENT_HASSUBFOLDERS:
case EVOLUTION_SHELL_COMPONENT_INTERNALERROR:
case EVOLUTION_SHELL_COMPONENT_INTERRUPTED:
case EVOLUTION_SHELL_COMPONENT_INVALIDARG:
case EVOLUTION_SHELL_COMPONENT_INVALIDURI:
case EVOLUTION_SHELL_COMPONENT_NOSPACE:
case EVOLUTION_SHELL_COMPONENT_NOTOWNED:
case EVOLUTION_SHELL_COMPONENT_UNKNOWNERROR:
default:
return E_STORAGE_GENERICERROR;
}
CORBA_exception_init (&ev);
Bonobo_Listener_event (listener, "evolution-shell:folder_created",
&any, &ev);
CORBA_exception_free (&ev);
}
@ -336,27 +359,6 @@ struct _AsyncCreateFolderCallbackData {
};
typedef struct _AsyncCreateFolderCallbackData AsyncCreateFolderCallbackData;
static void
notify_listener (const Bonobo_Listener listener,
EStorageResult result,
const char *physical_path)
{
CORBA_any any;
GNOME_Evolution_Storage_FolderResult folder_result;
CORBA_Environment ev;
folder_result.result = result;
folder_result.path = CORBA_string_dup (physical_path ?
physical_path : "");
any._type = TC_GNOME_Evolution_Storage_FolderResult;
any._value = &folder_result;
CORBA_exception_init (&ev);
Bonobo_Listener_event (listener, "evolution-shell:folder_created",
&any, &ev);
CORBA_exception_free (&ev);
}
static void
component_async_create_folder_callback (EvolutionShellComponentClient *shell_component_client,
EvolutionShellComponentResult result,
@ -382,7 +384,8 @@ component_async_create_folder_callback (EvolutionShellComponentClient *shell_com
e_folder_set_physical_uri (folder, callback_data->physical_uri);
if (e_local_folder_save (E_LOCAL_FOLDER (folder))) {
new_folder (E_LOCAL_STORAGE(callback_data->storage), callback_data->path, folder);
new_folder (E_LOCAL_STORAGE(callback_data->storage),
callback_data->path, folder);
} else {
rmdir (callback_data->physical_path);
gtk_object_unref (GTK_OBJECT (folder));
@ -393,8 +396,8 @@ component_async_create_folder_callback (EvolutionShellComponentClient *shell_com
bonobo_object_unref (BONOBO_OBJECT (shell_component_client));
if (callback_data->listener != CORBA_OBJECT_NIL)
notify_listener (callback_data->listener, storage_result,
callback_data->physical_path);
notify_bonobo_listener (callback_data->listener, storage_result,
callback_data->physical_path);
if (callback_data->callback != NULL)
(* callback_data->callback) (callback_data->storage,
@ -410,6 +413,134 @@ component_async_create_folder_callback (EvolutionShellComponentClient *shell_com
g_free (callback_data);
}
/* Implementation for the folder operations. */
static EStorageResult
create_folder_directory (ELocalStorage *local_storage,
const char *path,
const char *type,
const char *description,
char **physical_path_return)
{
EStorage *storage;
ELocalStoragePrivate *priv;
const char *folder_name;
char *physical_path;
char *parent_path;
storage = E_STORAGE (local_storage);
priv = local_storage->priv;
*physical_path_return = NULL;
g_assert (g_path_is_absolute (path));
folder_name = g_basename (path);
if (folder_name == path + 1) {
/* We want a direct child of the root, so we don't need to create a
`subfolders' directory. */
physical_path = get_physical_path (local_storage, path);
parent_path = g_strdup (G_DIR_SEPARATOR_S);
} else {
char *parent_physical_path;
char *subfolders_directory_physical_path;
/* Create the `subfolders' subdirectory under the parent. */
parent_path = g_strndup (path, folder_name - path - 1);
parent_physical_path = get_physical_path (local_storage, parent_path);
subfolders_directory_physical_path = g_concat_dir_and_file (parent_physical_path,
SUBFOLDER_DIR_NAME);
physical_path = g_concat_dir_and_file (subfolders_directory_physical_path,
folder_name);
g_free (subfolders_directory_physical_path);
g_free (parent_physical_path);
}
/* Create the directory that holds the folder. */
if (mkdir (physical_path, 0700) == -1) {
g_free (physical_path);
return errno_to_storage_result ();
}
*physical_path_return = physical_path;
return E_STORAGE_OK;
}
static void
create_folder (ELocalStorage *local_storage,
Bonobo_Listener listener,
const char *path,
const char *type,
const char *description,
EStorageResultCallback callback,
void *data)
{
EStorage *storage;
ELocalStoragePrivate *priv;
EvolutionShellComponentClient *component_client;
AsyncCreateFolderCallbackData *callback_data;
EStorageResult result;
const char *folder_name;
char *physical_path;
char *physical_uri;
storage = E_STORAGE (local_storage);
priv = local_storage->priv;
component_client = e_folder_type_registry_get_handler_for_type (priv->folder_type_registry,
type);
if (component_client == NULL) {
if (callback != NULL)
(* callback) (storage, E_STORAGE_INVALIDTYPE, data);
if (listener != CORBA_OBJECT_NIL)
notify_bonobo_listener (listener, E_STORAGE_INVALIDTYPE, NULL);
return;
}
g_assert (g_path_is_absolute (path));
result = create_folder_directory (local_storage, path, type, description, &physical_path);
if (result != E_STORAGE_OK) {
if (callback != NULL)
(* callback) (storage, result, data);
if (listener != CORBA_OBJECT_NIL)
notify_bonobo_listener (listener, result, NULL);
return;
}
folder_name = g_basename (path);
/* Finally tell the component to do the job of creating the physical files in it. */
/* FIXME: We should put the operations on a queue so that we can cancel them when
the ELocalStorage is destroyed. */
physical_uri = g_strconcat ("file://", physical_path, NULL);
callback_data = g_new (AsyncCreateFolderCallbackData, 1);
callback_data->storage = E_STORAGE (local_storage);
callback_data->path = g_strdup (path);
callback_data->display_name = g_strdup (folder_name);
callback_data->type = g_strdup (type);
callback_data->description = g_strdup (description);
callback_data->physical_uri = physical_uri;
callback_data->physical_path = physical_path;
callback_data->listener = listener;
callback_data->callback = callback;
callback_data->callback_data = data;
bonobo_object_ref (BONOBO_OBJECT (component_client));
evolution_shell_component_client_async_create_folder (component_client,
physical_uri,
type,
component_async_create_folder_callback,
callback_data);
}
/* GtkObject methods. */
@ -449,149 +580,6 @@ impl_get_name (EStorage *storage)
return E_LOCAL_STORAGE_NAME;
}
const char *invalid_names[6] = {
"Calendar",
"Contacts",
"Trash",
"Executive-Summary",
"Tasks",
NULL};
/* Checks that @foldername isn't an invalid name like Trash, Calendar etc */
static gboolean
check_valid_name (const char *foldername)
{
int i;
g_return_val_if_fail (foldername != NULL, FALSE);
for (i = 0; invalid_names[i] != NULL; i++) {
if (strcmp (invalid_names[i], foldername) == 0)
return FALSE;
}
return TRUE;
}
static void
real_do_folder_create (ELocalStorage *local_storage,
Bonobo_Listener listener,
const char *path,
const char *type,
const char *description,
EStorageResultCallback callback,
void *data)
{
EStorage *storage;
ELocalStoragePrivate *priv;
EvolutionShellComponentClient *component_client;
AsyncCreateFolderCallbackData *callback_data;
const char *folder_name;
char *physical_path;
char *physical_uri;
char *parent_path;
storage = E_STORAGE (local_storage);
priv = local_storage->priv;
component_client = e_folder_type_registry_get_handler_for_type (priv->folder_type_registry,
type);
if (component_client == NULL) {
if (callback != NULL)
(* callback) (storage, E_STORAGE_INVALIDTYPE, data);
notify_listener (listener, E_STORAGE_INVALIDTYPE, NULL);
return;
}
g_assert (g_path_is_absolute (path));
folder_name = g_basename (path);
/* Some validity checks */
if (!check_valid_name (folder_name)) {
if (callback != NULL)
(*callback) (storage, E_STORAGE_INVALIDNAME, data);
notify_listener (listener, E_STORAGE_INVALIDNAME, NULL);
return;
}
if (folder_name == path + 1) {
/* We want a direct child of the root, so we don't need to create a
`subfolders' directory. */
physical_path = get_physical_path (local_storage, path);
parent_path = g_strdup (G_DIR_SEPARATOR_S);
} else {
char *parent_physical_path;
char *subfolders_directory_physical_path;
/* Create the `subfolders' subdirectory under the parent. */
parent_path = g_strndup (path, folder_name - path - 1);
parent_physical_path = get_physical_path (local_storage, parent_path);
subfolders_directory_physical_path = g_concat_dir_and_file (parent_physical_path,
SUBFOLDER_DIR_NAME);
if (! g_file_exists (subfolders_directory_physical_path)
&& mkdir (subfolders_directory_physical_path, 0700) == -1) {
if (errno != EEXIST) {
/* Really bad error which we can't recover from */
g_free (parent_path);
g_free (subfolders_directory_physical_path);
g_free (parent_physical_path);
if (callback != NULL)
(* callback) (storage,
errno_to_storage_result (),
data);
notify_listener (listener,
errno_to_storage_result (), NULL);
return;
}
}
physical_path = g_concat_dir_and_file (subfolders_directory_physical_path,
folder_name);
g_free (subfolders_directory_physical_path);
g_free (parent_physical_path);
}
/* Create the directory that holds the folder. */
if (mkdir (physical_path, 0700) == -1) {
notify_listener (listener, errno_to_storage_result (),
physical_path);
g_free (physical_path);
if (callback != NULL)
(* callback) (storage,
errno_to_storage_result (), data);
return;
}
/* Finally tell the component to do the job of creating the physical files in
it. */
/* FIXME: We should put the operations on a queue so that we can cancel them when
the ELocalStorage is destroyed. */
physical_uri = g_strconcat ("file://", physical_path, NULL);
callback_data = g_new (AsyncCreateFolderCallbackData, 1);
callback_data->storage = E_STORAGE (local_storage);
callback_data->path = g_strdup (path);
callback_data->display_name = g_strdup (folder_name);
callback_data->type = g_strdup (type);
callback_data->description = g_strdup (description);
callback_data->physical_uri = physical_uri;
callback_data->physical_path = physical_path;
callback_data->listener = listener;
callback_data->callback = callback;
callback_data->callback_data = data;
bonobo_object_ref (BONOBO_OBJECT (component_client));
evolution_shell_component_client_async_create_folder (component_client,
physical_path,
type,
component_async_create_folder_callback,
callback_data);
}
static void
impl_async_create_folder (EStorage *storage,
const char *path,
@ -603,8 +591,8 @@ impl_async_create_folder (EStorage *storage,
ELocalStorage *local_storage;
local_storage = E_LOCAL_STORAGE (storage);
real_do_folder_create (local_storage, NULL, path, type,
description, callback, data);
create_folder (local_storage, NULL, path, type, description, callback, data);
}
static void
@ -618,22 +606,32 @@ impl_async_remove_folder (EStorage *storage,
local_storage = E_LOCAL_STORAGE (storage);
}
static void
impl_async_xfer_folder (EStorage *storage,
const char *source_path,
const char *destination_path,
const gboolean remove_source,
EStorageResultCallback callback,
void *data)
{
}
/* Callbacks for the `Evolution::LocalStorage' interface we are exposing to the outside world. */
static void
bonobo_interface_create_folder_cb (EvolutionStorage *estorage,
bonobo_interface_create_folder_cb (EvolutionStorage *storage,
const Bonobo_Listener listener,
const char *path,
const char *type,
const char *description,
const char *parent_p_path,
const char *parent_path,
void *data)
{
ELocalStorage *local_storage;
local_storage = E_LOCAL_STORAGE (data);
real_do_folder_create (local_storage, listener, path, type,
description, NULL, data);
create_folder (local_storage, listener, path, type, description, NULL, NULL);
}
static void
@ -674,6 +672,7 @@ class_init (ELocalStorageClass *class)
storage_class->get_name = impl_get_name;
storage_class->async_create_folder = impl_async_create_folder;
storage_class->async_remove_folder = impl_async_remove_folder;
storage_class->async_xfer_folder = impl_async_xfer_folder;
}
static void