From a4515de466908ffe7125b3d737b43f8b52be0195 Mon Sep 17 00:00:00 2001 From: djcb Date: Fri, 20 Aug 2010 21:07:36 +0300 Subject: [PATCH] * update: some refactoring of commands --- src/Makefile.am | 4 +- src/mu-cmd-find.c | 78 +----------------- src/mu-cmd-index.c | 2 +- src/mu-cmd-index.h | 47 ----------- src/{mu-cmd-find.h => mu-cmd-mkdir.c} | 57 +++++++------ src/mu-cmd-view.c | 110 ++++++++++++++++++++++++++ src/mu-cmd.c | 35 +------- src/mu-cmd.h | 54 ++++++++++++- 8 files changed, 202 insertions(+), 185 deletions(-) delete mode 100644 src/mu-cmd-index.h rename src/{mu-cmd-find.h => mu-cmd-mkdir.c} (52%) create mode 100644 src/mu-cmd-view.c diff --git a/src/Makefile.am b/src/Makefile.am index c5357984..46d2030e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,10 +48,10 @@ mu_LDADD= \ libmu_la_SOURCES= \ mu-cmd.c \ mu-cmd.h \ - mu-cmd-find.h \ mu-cmd-find.c \ - mu-cmd-index.h \ mu-cmd-index.c \ + mu-cmd-view.c \ + mu-cmd-mkdir.c \ mu-config.c \ mu-config.h \ mu-index.c \ diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index 7a8ba9fa..efdb562f 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -35,7 +35,7 @@ #include "mu-util.h" #include "mu-util-xapian.h" -#include "mu-cmd-find.h" +#include "mu-cmd.h" static void @@ -358,79 +358,3 @@ mu_cmd_find (MuConfigOptions *opts) return rv; } -/* we ignore fields for now */ -static gboolean -view_file (const gchar *path, const gchar *fields, size_t summary_len) -{ - MuMsgGMime* msg; - const char *field; - time_t date; - - msg = mu_msg_gmime_new (path, NULL); - if (!msg) - return FALSE; - - field = mu_msg_gmime_get_from (msg); - if (field) - g_print ("From: %s\n", field); - - field = mu_msg_gmime_get_to (msg); - if (field) - g_print ("To: %s\n", field); - - field = mu_msg_gmime_get_cc (msg); - if (field) - g_print ("Cc: %s\n", field); - - field = mu_msg_gmime_get_subject (msg); - if (field) - g_print ("Subject: %s\n", field); - - date = mu_msg_gmime_get_date (msg); - if (date) - g_print ("Date: %s\n", - mu_msg_str_date_s (date)); - - if (summary_len > 0) { - field = mu_msg_gmime_get_summary (msg, summary_len); - g_print ("Summary: %s\n", field ? field : ""); - } else { - - field = mu_msg_gmime_get_body_text (msg); - if (field) - g_print ("\n%s\n", field); - else - /* not really an error */ - g_debug ("No text body found for %s", path); - } - - mu_msg_gmime_destroy (msg); - - return TRUE; -} - -gboolean -mu_cmd_view (MuConfigOptions *opts) -{ - gboolean rv; - int i; - - g_return_val_if_fail (opts, FALSE); - - /* note: params[0] will be 'view' */ - if (!opts->params[0] || !opts->params[1]) { - g_printerr ("Missing files to view\n"); - return FALSE; - } - - mu_msg_gmime_init(); - - rv = TRUE; - for (i = 1; opts->params[i] && rv; ++i) - rv = view_file (opts->params[i], NULL, - opts->summary_len); - - mu_msg_gmime_uninit(); - - return rv; -} diff --git a/src/mu-cmd-index.c b/src/mu-cmd-index.c index 0c756518..fca67c2f 100644 --- a/src/mu-cmd-index.c +++ b/src/mu-cmd-index.c @@ -30,7 +30,7 @@ #include "mu-msg-gmime.h" #include "mu-index.h" -#include "mu-cmd-index.h" +#include "mu-cmd.h" static gboolean MU_CAUGHT_SIGNAL; diff --git a/src/mu-cmd-index.h b/src/mu-cmd-index.h deleted file mode 100644 index 7461eb99..00000000 --- a/src/mu-cmd-index.h +++ /dev/null @@ -1,47 +0,0 @@ -/* -** Copyright (C) 2010 Dirk-Jan C. Binnema -** -** This program is free software; you can redistribute it and/or modify it -** under the terms of the GNU General Public License as published by the -** Free Software Foundation; either version 3, or (at your option) any -** later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software Foundation, -** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -** -*/ - -#ifndef __MU_CMD_INDEX_H__ -#define __MU_CMD_INDEX_H__ - -#include -#include "mu-config.h" - -/** - * execute the 'index' command - * - * @param opts configuration options - * - * @return TRUE if the command succeede, FALSE otherwise - */ -gboolean mu_cmd_index (MuConfigOptions *opts); - - -/** - * execute the 'cleanup' command - * - * @param opts configuration options - * - * @return TRUE if the command succeede, FALSE otherwise - */ -gboolean mu_cmd_cleanup (MuConfigOptions *opts); - - - -#endif /*__MU_CMD_INDEX_H__*/ diff --git a/src/mu-cmd-find.h b/src/mu-cmd-mkdir.c similarity index 52% rename from src/mu-cmd-find.h rename to src/mu-cmd-mkdir.c index 786b169e..4c6398f6 100644 --- a/src/mu-cmd-find.h +++ b/src/mu-cmd-mkdir.c @@ -1,5 +1,5 @@ /* -** Copyright (C) 2010 Dirk-Jan C. Binnema +** Copyright (C) 2008-2010 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -17,30 +17,41 @@ ** */ -#ifndef __MU_CMD_FIND_H__ -#define __MU_CMD_FIND_H__ +#include "config.h" -#include -#include "mu-config.h" +#include +#include +#include +#include +#include -/** - * execute the 'find' command - * - * @param opts configuration options - * - * @return TRUE if the command succeede, FALSE otherwise - */ -gboolean mu_cmd_find (MuConfigOptions *opts); +#include "mu-maildir.h" +#include "mu-cmd.h" +#include "mu-util.h" -/** - * execute the 'view' command - * - * @param opts configuration options - * - * @return TRUE if the command succeede, FALSE otherwise - */ -gboolean mu_cmd_view (MuConfigOptions *opts); +gboolean +mu_cmd_mkdir (MuConfigOptions *opts) +{ + int i; + + if (!opts->params[0]) + return FALSE; /* shouldn't happen */ + + if (!opts->params[1]) { + g_printerr ( + "usage: mu mkdir [-u,--mode=] " + " [more dirs]\n"); + return FALSE; + } + + i = 1; + while (opts->params[i]) { + if (!mu_maildir_mkmdir (opts->params[i], opts->dirmode, + FALSE)) + return FALSE; + ++i; + } - -#endif /*__MU_CMD_FIND_H__*/ + return TRUE; +} diff --git a/src/mu-cmd-view.c b/src/mu-cmd-view.c new file mode 100644 index 00000000..648ce4ce --- /dev/null +++ b/src/mu-cmd-view.c @@ -0,0 +1,110 @@ +/* +** Copyright (C) 2008-2010 Dirk-Jan C. Binnema +** +** This program is free software; you can redistribute it and/or modify it +** under the terms of the GNU General Public License as published by the +** Free Software Foundation; either version 3, or (at your option) any +** later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include "mu-msg-gmime.h" +#include "mu-msg-str.h" +#include "mu-cmd.h" + +#include "mu-util.h" + + +/* we ignore fields for now */ +static gboolean +view_file (const gchar *path, const gchar *fields, size_t summary_len) +{ + MuMsgGMime* msg; + const char *field; + time_t date; + + msg = mu_msg_gmime_new (path, NULL); + if (!msg) + return FALSE; + + field = mu_msg_gmime_get_from (msg); + if (field) + g_print ("From: %s\n", field); + + field = mu_msg_gmime_get_to (msg); + if (field) + g_print ("To: %s\n", field); + + field = mu_msg_gmime_get_cc (msg); + if (field) + g_print ("Cc: %s\n", field); + + field = mu_msg_gmime_get_subject (msg); + if (field) + g_print ("Subject: %s\n", field); + + date = mu_msg_gmime_get_date (msg); + if (date) + g_print ("Date: %s\n", + mu_msg_str_date_s (date)); + + if (summary_len > 0) { + field = mu_msg_gmime_get_summary (msg, summary_len); + g_print ("Summary: %s\n", field ? field : ""); + } else { + + field = mu_msg_gmime_get_body_text (msg); + if (field) + g_print ("\n%s\n", field); + else + /* not really an error */ + g_debug ("No text body found for %s", path); + } + + mu_msg_gmime_destroy (msg); + + return TRUE; +} + +gboolean +mu_cmd_view (MuConfigOptions *opts) +{ + gboolean rv; + int i; + + g_return_val_if_fail (opts, FALSE); + + /* note: params[0] will be 'view' */ + if (!opts->params[0] || !opts->params[1]) { + g_printerr ("Missing files to view\n"); + return FALSE; + } + + mu_msg_gmime_init(); + + rv = TRUE; + for (i = 1; opts->params[i] && rv; ++i) + rv = view_file (opts->params[i], NULL, + opts->summary_len); + + mu_msg_gmime_uninit(); + + return rv; +} diff --git a/src/mu-cmd.c b/src/mu-cmd.c index 952acc20..166fab0c 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -26,13 +26,8 @@ #include #include "mu-maildir.h" - -#include "mu-cmd-index.h" -#include "mu-cmd-find.h" - #include "mu-cmd.h" - static MuCmd cmd_from_string (const char* cmd) { @@ -57,34 +52,6 @@ cmd_from_string (const char* cmd) return MU_CMD_UNKNOWN; } - - -static int -cmd_mkdir (MuConfigOptions *opts) -{ - int i; - - if (!opts->params[0]) - return FALSE; /* shouldn't happen */ - - if (!opts->params[1]) { - g_printerr ( - "usage: mu mkdir [-u,--mode=] " - " [more dirs]\n"); - return FALSE; - } - - i = 1; - while (opts->params[i]) { - if (!mu_maildir_mkmdir (opts->params[i], opts->dirmode, - FALSE)) - return FALSE; - ++i; - } - - return TRUE; -} - static gboolean show_usage (gboolean noerror) { @@ -142,7 +109,7 @@ mu_cmd_execute (MuConfigOptions *opts) case MU_CMD_INDEX: return mu_cmd_index (opts); case MU_CMD_FIND: return mu_cmd_find (opts); - case MU_CMD_MKDIR: return cmd_mkdir (opts); + case MU_CMD_MKDIR: return mu_cmd_mkdir (opts); case MU_CMD_CLEANUP: return mu_cmd_cleanup (opts); case MU_CMD_VIEW: return mu_cmd_view (opts); diff --git a/src/mu-cmd.h b/src/mu-cmd.h index 576aa1e2..10154a63 100644 --- a/src/mu-cmd.h +++ b/src/mu-cmd.h @@ -38,7 +38,7 @@ enum _MuCmd { }; typedef enum _MuCmd MuCmd; -/** +/** * try to execute whatever is specified on the command line * * @param config a config structure with the command line params @@ -47,6 +47,58 @@ typedef enum _MuCmd MuCmd; */ gboolean mu_cmd_execute (MuConfigOptions *config); + + +/** + * execute the 'mkdir' command + * + * @param opts configuration options + * + * @return TRUE if the command succeeded, FALSE otherwise + */ +gboolean mu_cmd_mkdir (MuConfigOptions *opts); + + +/** + * execute the 'view' command + * + * @param opts configuration options + * + * @return TRUE if the command succeeded, FALSE otherwise + */ +gboolean mu_cmd_view (MuConfigOptions *opts); + + +/** + * execute the 'index' command + * + * @param opts configuration options + * + * @return TRUE if the command succeede, FALSE otherwise + */ +gboolean mu_cmd_index (MuConfigOptions *opts); + + +/** + * execute the 'cleanup' command + * + * @param opts configuration options + * + * @return TRUE if the command succeede, FALSE otherwise + */ +gboolean mu_cmd_cleanup (MuConfigOptions *opts); + + +/** + * execute the 'find' command + * + * @param opts configuration options + * + * @return TRUE if the command succeede, FALSE otherwise + */ +gboolean mu_cmd_find (MuConfigOptions *opts); + + G_END_DECLS #endif /*__MU_CMD_H__*/