home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / include / libpurple / log.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-04  |  18.6 KB  |  569 lines

  1. /**
  2.  * @file log.h Logging API
  3.  * @ingroup core
  4.  *
  5.  * purple
  6.  *
  7.  * Purple is the legal property of its developers, whose names are too numerous
  8.  * to list here.  Please refer to the COPYRIGHT file distributed with this
  9.  * source distribution.
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24.  */
  25. #ifndef _PURPLE_LOG_H_
  26. #define _PURPLE_LOG_H_
  27.  
  28. #include <stdio.h>
  29.  
  30.  
  31. /********************************************************
  32.  * DATA STRUCTURES **************************************
  33.  ********************************************************/
  34.  
  35. typedef struct _PurpleLog PurpleLog;
  36. typedef struct _PurpleLogLogger PurpleLogLogger;
  37. typedef struct _PurpleLogCommonLoggerData PurpleLogCommonLoggerData;
  38. typedef struct _PurpleLogSet PurpleLogSet;
  39.  
  40. typedef enum {
  41.     PURPLE_LOG_IM,
  42.     PURPLE_LOG_CHAT,
  43.     PURPLE_LOG_SYSTEM
  44. } PurpleLogType;
  45.  
  46. typedef enum {
  47.     PURPLE_LOG_READ_NO_NEWLINE = 1
  48. } PurpleLogReadFlags;
  49.  
  50. #include "account.h"
  51. #include "conversation.h"
  52.  
  53. typedef void (*PurpleLogSetCallback) (GHashTable *sets, PurpleLogSet *set);
  54.  
  55. /**
  56.  * A log logger.
  57.  *
  58.  * This struct gets filled out and is included in the PurpleLog.  It contains everything
  59.  * needed to write and read from logs.
  60.  */
  61. struct _PurpleLogLogger {
  62.     char *name;               /**< The logger's name */
  63.     char *id;                 /**< an identifier to refer to this logger */
  64.  
  65.     /** This gets called when the log is first created.
  66.         I don't think this is actually needed. */
  67.     void (*create)(PurpleLog *log);
  68.  
  69.     /** This is used to write to the log file */
  70.     gsize (*write)(PurpleLog *log,
  71.              PurpleMessageFlags type,
  72.              const char *from,
  73.              time_t time,
  74.              const char *message);
  75.  
  76.     /** Called when the log is destroyed */
  77.     void (*finalize)(PurpleLog *log);
  78.  
  79.     /** This function returns a sorted GList of available PurpleLogs */
  80.     GList *(*list)(PurpleLogType type, const char *name, PurpleAccount *account);
  81.  
  82.     /** Given one of the logs returned by the logger's list function,
  83.      *  this returns the contents of the log in GtkIMHtml markup */
  84.     char *(*read)(PurpleLog *log, PurpleLogReadFlags *flags);
  85.  
  86.     /** Given one of the logs returned by the logger's list function,
  87.      *  this returns the size of the log in bytes */
  88.     int (*size)(PurpleLog *log);
  89.  
  90.     /** Returns the total size of all the logs. If this is undefined a default
  91.      *  implementation is used */
  92.     int (*total_size)(PurpleLogType type, const char *name, PurpleAccount *account);
  93.  
  94.     /** This function returns a sorted GList of available system PurpleLogs */
  95.     GList *(*list_syslog)(PurpleAccount *account);
  96.  
  97.     /** Adds PurpleLogSets to a GHashTable. By passing the data in the PurpleLogSets
  98.      *  to list, the caller can get every available PurpleLog from the logger.
  99.      *  Loggers using purple_log_common_writer() (or otherwise storing their
  100.      *  logs in the same directory structure as the stock loggers) do not
  101.      *  need to implement this function.
  102.      *
  103.      *  Loggers which implement this function must create a PurpleLogSet,
  104.      *  then call @a cb with @a sets and the newly created PurpleLogSet. */
  105.     void (*get_log_sets)(PurpleLogSetCallback cb, GHashTable *sets);
  106.  
  107.     /* Attempts to delete the specified log, indicating success or failure */
  108.     gboolean (*remove)(PurpleLog *log);
  109.  
  110.     /* Tests whether a log is deletable */
  111.     gboolean (*is_deletable)(PurpleLog *log);
  112.  
  113.     void (*_purple_reserved1)(void);
  114.     void (*_purple_reserved2)(void);
  115.     void (*_purple_reserved3)(void);
  116.     void (*_purple_reserved4)(void);
  117. };
  118.  
  119. /**
  120.  * A log.  Not the wooden type.
  121.  */
  122. struct _PurpleLog {
  123.     PurpleLogType type;                     /**< The type of log this is */
  124.     char *name;                           /**< The name of this log */
  125.     PurpleAccount *account;                 /**< The account this log is taking
  126.                                                place on */
  127.     PurpleConversation *conv;               /**< The conversation being logged */
  128.     time_t time;                          /**< The time this conversation
  129.                                                started, converted to the local timezone */
  130.  
  131.     PurpleLogLogger *logger;                /**< The logging mechanism this log
  132.                                                is to use */
  133.     void *logger_data;                    /**< Data used by the log logger */
  134.     struct tm *tm;                        /**< The time this conversation
  135.                                                started, saved with original
  136.                                                timezone data, if available and
  137.                                                if struct tm has the BSD
  138.                                                timezone fields, else @c NULL.
  139.                                                Do NOT modify anything in this struct.*/
  140.  
  141.     /* IMPORTANT: Some code in log.c allocates these without zeroing them.
  142.      * IMPORTANT: Update that code if you add members here. */
  143. };
  144.  
  145. /**
  146.  * A common logger_data struct containing a file handle and path, as well
  147.  * as a pointer to something else for additional data.
  148.  */
  149. struct _PurpleLogCommonLoggerData {
  150.     char *path;
  151.     FILE *file;
  152.     void *extra_data;
  153. };
  154.  
  155. /**
  156.  * Describes available logs.
  157.  *
  158.  * By passing the elements of this struct to purple_log_get_logs(), the caller
  159.  * can get all available PurpleLogs.
  160.  */
  161. struct _PurpleLogSet {
  162.     PurpleLogType type;                     /**< The type of logs available */
  163.     char *name;                           /**< The name of the logs available */
  164.     PurpleAccount *account;                 /**< The account the available logs
  165.                                                took place on. This will be
  166.                                                @c NULL if the account no longer
  167.                                                exists. (Depending on a
  168.                                                logger's implementation of
  169.                                                list, it may not be possible
  170.                                                to load such logs.) */
  171.     gboolean buddy;                       /**< Is this (account, name) a buddy
  172.                                                on the buddy list? */
  173.     char *normalized_name;                /**< The normalized version of
  174.                                                @a name. It must be set, and
  175.                                                may be set to the same pointer
  176.                                                value as @a name. */
  177.  
  178.     /* IMPORTANT: Some code in log.c allocates these without zeroing them.
  179.      * IMPORTANT: Update that code if you add members here. */
  180. };
  181.  
  182. #ifdef __cplusplus
  183. extern "C" {
  184. #endif
  185.  
  186. /***************************************/
  187. /** @name Log Functions                */
  188. /***************************************/
  189. /*@{*/
  190.  
  191. /**
  192.  * Creates a new log
  193.  *
  194.  * @param type        The type of log this is.
  195.  * @param name        The name of this conversation (screenname, chat name,
  196.  *                    etc.)
  197.  * @param account     The account the conversation is occurring on
  198.  * @param conv        The conversation being logged
  199.  * @param time        The time this conversation started
  200.  * @param tm          The time this conversation started, with timezone data,
  201.  *                    if available and if struct tm has the BSD timezone fields.
  202.  * @return            The new log
  203.  */
  204. PurpleLog *purple_log_new(PurpleLogType type, const char *name, PurpleAccount *account,
  205.                       PurpleConversation *conv, time_t time, const struct tm *tm);
  206.  
  207. /**
  208.  * Frees a log
  209.  *
  210.  * @param log         The log to destroy
  211.  */
  212. void purple_log_free(PurpleLog *log);
  213.  
  214. /**
  215.  * Writes to a log file. Assumes you have checked preferences already.
  216.  *
  217.  * @param log          The log to write to
  218.  * @param type         The type of message being logged
  219.  * @param from         Whom this message is coming from, or @c NULL for
  220.  *                     system messages
  221.  * @param time         A timestamp in UNIX time
  222.  * @param message      The message to log
  223.  */
  224. void purple_log_write(PurpleLog *log,
  225.             PurpleMessageFlags type,
  226.             const char *from,
  227.             time_t time,
  228.             const char *message);
  229.  
  230. /**
  231.  * Reads from a log
  232.  *
  233.  * @param log   The log to read from
  234.  * @param flags The returned logging flags.
  235.  *
  236.  * @return The contents of this log in Purple Markup.
  237.  */
  238. char *purple_log_read(PurpleLog *log, PurpleLogReadFlags *flags);
  239.  
  240. /**
  241.  * Returns a list of all available logs
  242.  *
  243.  * @param type                The type of the log
  244.  * @param name                The name of the log
  245.  * @param account             The account
  246.  * @return                    A sorted list of PurpleLogs
  247.  */
  248. GList *purple_log_get_logs(PurpleLogType type, const char *name, PurpleAccount *account);
  249.  
  250. /**
  251.  * Returns a GHashTable of PurpleLogSets.
  252.  *
  253.  * A "log set" here means the information necessary to gather the
  254.  * PurpleLogs for a given buddy/chat. This information would be passed
  255.  * to purple_log_list to get a list of PurpleLogs.
  256.  *
  257.  * The primary use of this function is to get a list of everyone the
  258.  * user has ever talked to (assuming he or she uses logging).
  259.  *
  260.  * The GHashTable that's returned will free all log sets in it when
  261.  * destroyed. If a PurpleLogSet is removed from the GHashTable, it
  262.  * must be freed with purple_log_set_free().
  263.  *
  264.  * @return A GHashTable of all available unique PurpleLogSets
  265.  */
  266. GHashTable *purple_log_get_log_sets(void);
  267.  
  268. /**
  269.  * Returns a list of all available system logs
  270.  *
  271.  * @param account The account
  272.  * @return        A sorted list of PurpleLogs
  273.  */
  274. GList *purple_log_get_system_logs(PurpleAccount *account);
  275.  
  276. /**
  277.  * Returns the size of a log
  278.  *
  279.  * @param log                 The log
  280.  * @return                    The size of the log, in bytes
  281.  */
  282. int purple_log_get_size(PurpleLog *log);
  283.  
  284. /**
  285.  * Returns the size, in bytes, of all available logs in this conversation
  286.  *
  287.  * @param type                The type of the log
  288.  * @param name                The name of the log
  289.  * @param account             The account
  290.  * @return                    The size in bytes
  291.  */
  292. int purple_log_get_total_size(PurpleLogType type, const char *name, PurpleAccount *account);
  293.  
  294. /**
  295.  * Tests whether a log is deletable
  296.  *
  297.  * A return value of @c FALSE indicates that purple_log_delete() will fail on this
  298.  * log, unless something changes between the two calls.  A return value of @c TRUE,
  299.  * however, does not guarantee the log can be deleted.
  300.  *
  301.  * @param log                 The log
  302.  * @return                    A boolean indicating if the log is deletable
  303.  */
  304. gboolean purple_log_is_deletable(PurpleLog *log);
  305.  
  306. /**
  307.  * Deletes a log
  308.  *
  309.  * @param log                 The log
  310.  * @return                    A boolean indicating success or failure
  311.  */
  312. gboolean purple_log_delete(PurpleLog *log);
  313.  
  314. /**
  315.  * Returns the default logger directory Purple uses for a given account
  316.  * and username.  This would be where Purple stores logs created by
  317.  * the built-in text or HTML loggers.
  318.  *
  319.  * @param type                The type of the log.
  320.  * @param name                The name of the log.
  321.  * @param account             The account.
  322.  * @return                    The default logger directory for Purple.
  323.  */
  324. char *purple_log_get_log_dir(PurpleLogType type, const char *name, PurpleAccount *account);
  325.  
  326. /**
  327.  * Implements GCompareFunc for PurpleLogs
  328.  *
  329.  * @param y                   A PurpleLog
  330.  * @param z                   Another PurpleLog
  331.  * @return                    A value as specified by GCompareFunc
  332.  */
  333. gint purple_log_compare(gconstpointer y, gconstpointer z);
  334.  
  335. /**
  336.  * Implements GCompareFunc for PurpleLogSets
  337.  *
  338.  * @param y                   A PurpleLogSet
  339.  * @param z                   Another PurpleLogSet
  340.  * @return                    A value as specified by GCompareFunc
  341.  */
  342. gint purple_log_set_compare(gconstpointer y, gconstpointer z);
  343.  
  344. /**
  345.  * Frees a log set
  346.  *
  347.  * @param set         The log set to destroy
  348.  */
  349. void purple_log_set_free(PurpleLogSet *set);
  350.  
  351. /*@}*/
  352.  
  353. /******************************************/
  354. /** @name Common Logger Functions         */
  355. /******************************************/
  356. /*@{*/
  357.  
  358. /**
  359.  * Opens a new log file in the standard Purple log location
  360.  * with the given file extension, named for the current time,
  361.  * for writing.  If a log file is already open, the existing
  362.  * file handle is retained.  The log's logger_data value is
  363.  * set to a PurpleLogCommonLoggerData struct containing the log
  364.  * file handle and log path.
  365.  *
  366.  * This function is intended to be used as a "common"
  367.  * implementation of a logger's @c write function.
  368.  * It should only be passed to purple_log_logger_new() and never
  369.  * called directly.
  370.  *
  371.  * @param log   The log to write to.
  372.  * @param ext   The file extension to give to this log file.
  373.  */
  374. void purple_log_common_writer(PurpleLog *log, const char *ext);
  375.  
  376. /**
  377.  * Returns a sorted GList of PurpleLogs of the requested type.
  378.  *
  379.  * This function should only be used with logs that are written
  380.  * with purple_log_common_writer().  It's intended to be used as
  381.  * a "common" implementation of a logger's @c list function.
  382.  * It should only be passed to purple_log_logger_new() and never
  383.  * called directly.
  384.  *
  385.  * @param type     The type of the logs being listed.
  386.  * @param name     The name of the log.
  387.  * @param account  The account of the log.
  388.  * @param ext      The file extension this log format uses.
  389.  * @param logger   A reference to the logger struct for this log.
  390.  *
  391.  * @return A sorted GList of PurpleLogs matching the parameters.
  392.  */
  393. GList *purple_log_common_lister(PurpleLogType type, const char *name,
  394.                               PurpleAccount *account, const char *ext,
  395.                               PurpleLogLogger *logger);
  396.  
  397. /**
  398.  * Returns the total size of all the logs for a given user, with
  399.  * a given extension.
  400.  *
  401.  * This function should only be used with logs that are written
  402.  * with purple_log_common_writer().  It's intended to be used as
  403.  * a "common" implementation of a logger's @c total_size function.
  404.  * It should only be passed to purple_log_logger_new() and never
  405.  * called directly.
  406.  *
  407.  * @param type     The type of the logs being sized.
  408.  * @param name     The name of the logs to size
  409.  *                 (e.g. the username or chat name).
  410.  * @param account  The account of the log.
  411.  * @param ext      The file extension this log format uses.
  412.  *
  413.  * @return The size of all the logs with the specified extension
  414.  *         for the specified user.
  415.  */
  416. int purple_log_common_total_sizer(PurpleLogType type, const char *name,
  417.                                 PurpleAccount *account, const char *ext);
  418.  
  419. /**
  420.  * Returns the size of a given PurpleLog.
  421.  *
  422.  * This function should only be used with logs that are written
  423.  * with purple_log_common_writer().  It's intended to be used as
  424.  * a "common" implementation of a logger's @c size function.
  425.  * It should only be passed to purple_log_logger_new() and never
  426.  * called directly.
  427.  *
  428.  * @param log      The PurpleLog to size.
  429.  *
  430.  * @return An integer indicating the size of the log in bytes.
  431.  */
  432. int purple_log_common_sizer(PurpleLog *log);
  433.  
  434. /**
  435.  * Deletes a log
  436.  *
  437.  * This function should only be used with logs that are written
  438.  * with purple_log_common_writer().  It's intended to be used as
  439.  * a "common" implementation of a logger's @c delete function.
  440.  * It should only be passed to purple_log_logger_new() and never
  441.  * called directly.
  442.  *
  443.  * @param log      The PurpleLog to delete.
  444.  *
  445.  * @return A boolean indicating success or failure.
  446.  */
  447. gboolean purple_log_common_deleter(PurpleLog *log);
  448.  
  449. /**
  450.  * Checks to see if a log is deletable
  451.  *
  452.  * This function should only be used with logs that are written
  453.  * with purple_log_common_writer().  It's intended to be used as
  454.  * a "common" implementation of a logger's @c is_deletable function.
  455.  * It should only be passed to purple_log_logger_new() and never
  456.  * called directly.
  457.  *
  458.  * @param log      The PurpleLog to check.
  459.  *
  460.  * @return A boolean indicating if the log is deletable.
  461.  */
  462. gboolean purple_log_common_is_deletable(PurpleLog *log);
  463.  
  464. /*@}*/
  465.  
  466. /******************************************/
  467. /** @name Logger Functions                */
  468. /******************************************/
  469. /*@{*/
  470.  
  471. /**
  472.  * Creates a new logger
  473.  *
  474.  * @param id           The logger's id.
  475.  * @param name         The logger's name.
  476.  * @param functions    The number of functions being passed. The following
  477.  *                     functions are currently available (in order): @c create,
  478.  *                     @c write, @c finalize, @c list, @c read, @c size,
  479.  *                     @c total_size, @c list_syslog, @c get_log_sets,
  480.  *                     @c remove, @c is_deletable.
  481.  *                     For details on these functions, see PurpleLogLogger.
  482.  *                     Functions may not be skipped. For example, passing
  483.  *                     @c create and @c write is acceptable (for a total of
  484.  *                     two functions). Passing @c create and @c finalize,
  485.  *                     however, is not. To accomplish that, the caller must
  486.  *                     pass @c create, @c NULL (a placeholder for @c write),
  487.  *                     and @c finalize (for a total of 3 functions).
  488.  *
  489.  * @return The new logger
  490.  */
  491. PurpleLogLogger *purple_log_logger_new(const char *id, const char *name, int functions, ...);
  492.  
  493. /**
  494.  * Frees a logger
  495.  *
  496.  * @param logger       The logger to free
  497.  */
  498. void purple_log_logger_free(PurpleLogLogger *logger);
  499.  
  500. /**
  501.  * Adds a new logger
  502.  *
  503.  * @param logger       The new logger to add
  504.  */
  505. void purple_log_logger_add (PurpleLogLogger *logger);
  506.  
  507. /**
  508.  *
  509.  * Removes a logger
  510.  *
  511.  * @param logger       The logger to remove
  512.  */
  513. void purple_log_logger_remove (PurpleLogLogger *logger);
  514.  
  515. /**
  516.  *
  517.  * Sets the current logger
  518.  *
  519.  * @param logger       The logger to set
  520.  */
  521. void purple_log_logger_set (PurpleLogLogger *logger);
  522.  
  523. /**
  524.  *
  525.  * Returns the current logger
  526.  *
  527.  * @return logger      The current logger
  528.  */
  529. PurpleLogLogger *purple_log_logger_get (void);
  530.  
  531. /**
  532.  * Returns a GList containing the IDs and names of the registered
  533.  * loggers.
  534.  *
  535.  * @return The list of IDs and names.
  536.  */
  537. GList *purple_log_logger_get_options(void);
  538.  
  539. /**************************************************************************/
  540. /** @name Log Subsystem                                                   */
  541. /**************************************************************************/
  542. /*@{*/
  543.  
  544. /**
  545.  * Initializes the log subsystem.
  546.  */
  547. void purple_log_init(void);
  548.  
  549. /**
  550.  * Returns the log subsystem handle.
  551.  *
  552.  * @return The log subsystem handle.
  553.  */
  554. void *purple_log_get_handle(void);
  555.  
  556. /**
  557.  * Uninitializes the log subsystem.
  558.  */
  559. void purple_log_uninit(void);
  560.  
  561. /*@}*/
  562.  
  563.  
  564. #ifdef __cplusplus
  565. }
  566. #endif
  567.  
  568. #endif /* _PURPLE_LOG_H_ */
  569.