changeset 15528:f61ad08739fc

This is the core code to support log deletion. It's untested.
author Richard Laager <rlaager@wiktel.com>
date Sat, 03 Feb 2007 21:43:29 +0000
parents aa0c1aa85fd3
children 443915ab77a1
files libpurple/log.c libpurple/log.h
diffstat 2 files changed, 183 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/log.c	Sat Feb 03 21:16:07 2007 +0000
+++ b/libpurple/log.c	Sat Feb 03 21:43:29 2007 +0000
@@ -248,6 +248,31 @@
 	return size;
 }
 
+gboolean gaim_log_is_deletable(GaimLog *log)
+{
+	g_return_val_if_fail(log != NULL, FALSE);
+	g_return_val_if_fail(log->logger != NULL, FALSE);
+
+	if (log->logger->delete == NULL)
+		return FALSE;
+
+	if (log->logger->is_deletable != NULL)
+		return log->logger->is_deletable(log);
+
+	return TRUE;
+}
+
+gboolean gaim_log_delete(GaimLog *log)
+{
+	g_return_val_if_fail(log != NULL, FALSE);
+	g_return_val_if_fail(log->logger != NULL, FALSE);
+
+	if (log->logger->delete != NULL)
+		return log->logger->delete(log);
+
+	return FALSE;
+}
+
 char *
 gaim_log_get_log_dir(GaimLogType type, const char *name, GaimAccount *account)
 {
@@ -318,7 +343,9 @@
 				int(*size)(GaimLog*),
 				int(*total_size)(GaimLogType type, const char *name, GaimAccount *account),
 				GList*(*list_syslog)(GaimAccount *account),
-				void(*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets))
+				void(*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets),
+				gboolean(*delete)(GaimLog *log),
+				gboolean(*is_deletable)(GaimLog *log))
 {
 #endif
 	GaimLogLogger *logger;
@@ -352,8 +379,12 @@
 		logger->list_syslog = va_arg(args, void *);
 	if (functions >= 9)
 		logger->get_log_sets = va_arg(args, void *);
+	if (functions >= 10)
+		logger->delete = va_arg(args, void *);
+	if (functions >= 11)
+		logger->is_deletable = va_arg(args, void *);
 
-	if (functions > 9)
+	if (functions >= 12)
 		gaim_debug_info("log", "Dropping new functions for logger: %s (%s)\n", name, id);
 
 	va_end(args);
@@ -567,7 +598,10 @@
 									  html_logger_read,
 									  gaim_log_common_sizer,
 									  html_logger_total_size,
-									  html_logger_list_syslog);
+									  html_logger_list_syslog,
+									  NULL,
+									  gaim_log_common_deleter,
+									  gaim_log_common_is_deletable);
 	gaim_log_logger_add(html_logger);
 
 	txt_logger = gaim_log_logger_new("txt", _("Plain text"), 8,
@@ -578,7 +612,10 @@
 									 txt_logger_read,
 									 gaim_log_common_sizer,
 									 txt_logger_total_size,
-									 txt_logger_list_syslog);
+									 txt_logger_list_syslog,
+									 NULL,
+									 gaim_log_common_deleter,
+									 gaim_log_common_is_deletable);
 	gaim_log_logger_add(txt_logger);
 
 	old_logger = gaim_log_logger_new("old", _("Old flat format"), 9,
@@ -817,6 +854,8 @@
 	struct stat st;
 	GaimLogCommonLoggerData *data = log->logger_data;
 
+	g_return_val_if_fail(data != NULL, 0);
+
 	if (!data->path || g_stat(data->path, &st))
 		st.st_size = 0;
 
@@ -943,6 +982,62 @@
 	g_dir_close(log_dir);
 }
 
+gboolean gaim_log_common_deleter(GaimLog *log)
+{
+	GaimLogCommonLoggerData *data;
+	int ret;
+
+	g_return_val_if_fail(log != NULL, FALSE);
+
+	data = log->logger_data;
+	if (data == NULL)
+		return FALSE;
+
+	if (data->path == NULL)
+		return FALSE;
+
+	ret = g_unlink(data->path);
+	if (ret == 0)
+		return TRUE;
+	else if (ret == -1)
+	{
+		gaim_debug_error("log", "Failed to delete: %s - %s\n", data->path, strerror(errno));
+	}
+	else
+	{
+		/* I'm not sure that g_unlink() will ever return
+		 * something other than 0 or -1. -- rlaager */
+		gaim_debug_error("log", "Failed to delete: %s\n", data->path);
+	}
+
+	return FALSE;
+}
+
+gboolean gaim_log_common_is_deletable(GaimLog *log)
+{
+	GaimLogCommonLoggerData *data;
+	gchar *dirname;
+
+	g_return_val_if_fail(log != NULL, FALSE);
+
+	data = log->logger_data;
+	if (data == NULL)
+		return FALSE;
+
+	if (data->path == NULL)
+		return FALSE;
+
+	dirname = g_path_get_dirname(data->path);
+	if (g_access(dirname, W_OK) == 0)
+	{
+		g_free(dirname);
+		return TRUE;
+	}
+	g_free(dirname);
+
+	return FALSE;
+}
+
 #if 0 /* Maybe some other time. */
 /****************
  ** XML LOGGER **
--- a/libpurple/log.h	Sat Feb 03 21:16:07 2007 +0000
+++ b/libpurple/log.h	Sat Feb 03 21:43:29 2007 +0000
@@ -80,15 +80,15 @@
 	GList *(*list)(GaimLogType type, const char *name, GaimAccount *account);
 
 	/** Given one of the logs returned by the logger's list function,
-	 * this returns the contents of the log in GtkIMHtml markup */
+	 *  this returns the contents of the log in GtkIMHtml markup */
 	char *(*read)(GaimLog *log, GaimLogReadFlags *flags);
 
 	/** Given one of the logs returned by the logger's list function,
-	 * this returns the size of the log in bytes */
+	 *  this returns the size of the log in bytes */
 	int (*size)(GaimLog *log);
 
 	/** Returns the total size of all the logs. If this is undefined a default
-	 * implementation is used */
+	 *  implementation is used */
 	int (*total_size)(GaimLogType type, const char *name, GaimAccount *account);
 
 	/** This function returns a sorted GList of available system GaimLogs */
@@ -103,6 +103,12 @@
 	 *  Loggers which implement this function must create a GaimLogSet,
 	 *  then call @a cb with @a sets and the newly created GaimLogSet. */
 	void (*get_log_sets)(GaimLogSetCallback cb, GHashTable *sets);
+
+	/* Attempts to delete the specified log, indicating success or failure */
+	gboolean (*delete)(GaimLog *log);
+
+	/* Tests whether a log is deletable */
+	gboolean (*is_deletable)(GaimLog *log);
 };
 
 /**
@@ -281,6 +287,26 @@
 int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account);
 
 /**
+ * Tests whether a log is deletable
+ *
+ * A return value of @c FALSE indicates that gaim_log_delete() will fail on this
+ * log, unless something changes between the two calls.  A return value of @c TRUE,
+ * however, does not guarantee the log can be deleted.
+ *
+ * @param log                 The log
+ * @return                    A boolean indicating if the log is deletable
+ */
+gboolean gaim_log_is_deletable(GaimLog *log);
+
+/**
+ * Deletes a log
+ *
+ * @param log                 The log
+ * @return                    A boolean indicating success or failure
+ */
+gboolean gaim_log_delete(GaimLog *log);
+
+/**
  * Returns the default logger directory Gaim uses for a given account
  * and username.  This would be where Gaim stores logs created by
  * the built-in text or HTML loggers.
@@ -332,6 +358,11 @@
  * set to a GaimLogCommonLoggerData struct containing the log
  * file handle and log path.
  *
+ * This function is intended to be used as a "common"
+ * implementation of a logger's @c write function.
+ * It should only be passed to gaim_log_logger_new() and never
+ * called directly.
+ *
  * @param log   The log to write to.
  * @param ext   The file extension to give to this log file.
  */
@@ -339,8 +370,12 @@
 
 /**
  * Returns a sorted GList of GaimLogs of the requested type.
+ *
  * This function should only be used with logs that are written
- * with gaim_log_common_writer().
+ * with gaim_log_common_writer().  It's intended to be used as
+ * a "common" implementation of a logger's @c list function.
+ * It should only be passed to gaim_log_logger_new() and never
+ * called directly.
  *
  * @param type     The type of the logs being listed.
  * @param name     The name of the log.
@@ -356,10 +391,13 @@
 
 /**
  * Returns the total size of all the logs for a given user, with
- * a given extension.  This is the "common" implemention of a
- * logger's total_size function.
+ * a given extension.
+ *
  * This function should only be used with logs that are written
- * with gaim_log_common_writer().
+ * with gaim_log_common_writer().  It's intended to be used as
+ * a "common" implementation of a logger's @c total_size function.
+ * It should only be passed to gaim_log_logger_new() and never
+ * called directly.
  *
  * @param type     The type of the logs being sized.
  * @param name     The name of the logs to size
@@ -375,14 +413,49 @@
 
 /**
  * Returns the size of a given GaimLog.
+ *
  * This function should only be used with logs that are written
- * with gaim_log_common_writer().
+ * with gaim_log_common_writer().  It's intended to be used as
+ * a "common" implementation of a logger's @c size function.
+ * It should only be passed to gaim_log_logger_new() and never
+ * called directly.
  *
  * @param log      The GaimLog to size.
  *
  * @return An integer indicating the size of the log in bytes.
  */
 int gaim_log_common_sizer(GaimLog *log);
+
+/**
+ * Deletes a log
+ *
+ * This function should only be used with logs that are written
+ * with gaim_log_common_writer().  It's intended to be used as
+ * a "common" implementation of a logger's @c delete function.
+ * It should only be passed to gaim_log_logger_new() and never
+ * called directly.
+ *
+ * @param log      The GaimLog to delete.
+ *
+ * @return A boolean indicating success or failure.
+ */
+gboolean gaim_log_common_deleter(GaimLog *log);
+
+/**
+ * Checks to see if a log is deletable
+ *
+ * This function should only be used with logs that are written
+ * with gaim_log_common_writer().  It's intended to be used as
+ * a "common" implementation of a logger's @c is_deletable function.
+ * It should only be passed to gaim_log_logger_new() and never
+ * called directly.
+ *
+ * @param log      The GaimLog to check.
+ *
+ * @return A boolean indicating if the log is deletable.
+ */
+gboolean gaim_log_common_is_deletable(GaimLog *log);
+
 /*@}*/
 
 /******************************************/
@@ -398,8 +471,9 @@
  * @param functions    The number of functions being passed. The following
  *                     functions are currently available (in order): @c create,
  *                     @c write, @c finalize, @c list, @c read, @c size,
- *                     @c total_size, @c list_syslog, @c get_log_sets. For
- *                     details on these functions, see GaimLogLogger.
+ *                     @c total_size, @c list_syslog, @c get_log_sets,
+ *                     @c delete, @c is_deletable.
+ *                     For details on these functions, see GaimLogLogger.
  *                     Functions may not be skipped. For example, passing
  *                     @c create and @c write is acceptable (for a total of
  *                     two functions). Passing @c create and @c finalize,