From 117e5bc76ea5210b9fa2802b229a70c684ad8d10 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 31 Oct 2010 11:34:01 +0200 Subject: [PATCH] * refactor output a bit (mu-cmd-find) --- src/Makefile.am | 4 ++ src/mu-cmd-find.c | 128 +++--------------------------------------- src/mu-output-link.c | 88 +++++++++++++++++++++++++++++ src/mu-output-link.h | 45 +++++++++++++++ src/mu-output-plain.c | 128 ++++++++++++++++++++++++++++++++++++++++++ src/mu-output-plain.h | 35 ++++++++++++ 6 files changed, 309 insertions(+), 119 deletions(-) create mode 100644 src/mu-output-link.c create mode 100644 src/mu-output-link.h create mode 100644 src/mu-output-plain.c create mode 100644 src/mu-output-plain.h diff --git a/src/Makefile.am b/src/Makefile.am index 688d730a..ebf14ea9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -80,6 +80,10 @@ libmu_la_SOURCES= \ mu-msg-str.c \ mu-msg-str.h \ mu-msg.h \ + mu-output-link.c \ + mu-output-link.h \ + mu-output-plain.c \ + mu-output-plain.h \ mu-query.cc \ mu-query.h \ mu-result.h \ diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index 90ce0cc7..bf328826 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -38,6 +38,8 @@ #include "mu-util.h" #include "mu-util-db.h" #include "mu-cmd.h" +#include "mu-output-plain.h" +#include "mu-output-link.h" static void @@ -64,43 +66,6 @@ print_xapian_query (MuQuery *xapian, const gchar *query) return TRUE; } - -static const gchar* -display_field (MuMsgIter *iter, const MuMsgField* field) -{ - gint64 val; - - switch (mu_msg_field_type(field)) { - case MU_MSG_FIELD_TYPE_STRING: - return mu_msg_iter_get_field (iter, field); - - case MU_MSG_FIELD_TYPE_INT: - - if (mu_msg_field_id(field) == MU_MSG_FIELD_ID_PRIO) { - val = mu_msg_iter_get_field_numeric (iter, field); - return mu_msg_str_prio ((MuMsgPrio)val); - } - - if (mu_msg_field_id(field) == MU_MSG_FIELD_ID_FLAGS) { - val = mu_msg_iter_get_field_numeric (iter, field); - return mu_msg_str_flags_s ((MuMsgPrio)val); - } - - return mu_msg_iter_get_field (iter, field); /* as string */ - - case MU_MSG_FIELD_TYPE_TIME_T: - val = mu_msg_iter_get_field_numeric (iter, field); - return mu_msg_str_date_s ((time_t)val); - - case MU_MSG_FIELD_TYPE_BYTESIZE: - val = mu_msg_iter_get_field_numeric (iter, field); - return mu_msg_str_size_s ((time_t)val); - default: - g_return_val_if_reached (NULL); - } -} - - /* returns NULL if there is an error */ const MuMsgField* sort_field_from_string (const char* fieldstr) @@ -118,116 +83,41 @@ sort_field_from_string (const char* fieldstr) return field; } -static void -print_summary (MuMsgIter *iter, size_t summary_len) -{ - const char *summ; - MuMsg *msg; - - msg = mu_msg_iter_get_msg (iter); - if (!msg) { - g_warning ("%s: failed to create msg object", __FUNCTION__); - return; - } - - summ = mu_msg_get_summary (msg, summary_len); - g_print ("Summary: %s\n", summ ? summ : ""); - - mu_msg_destroy (msg); -} - - static size_t print_rows (MuMsgIter *iter, const char *fields, size_t summary_len) { size_t count = 0; - const char* myfields; if (mu_msg_iter_is_done (iter)) return 0; do { - int len = 0; - - myfields = fields; - while (*myfields) { - const MuMsgField* field; - field = mu_msg_field_from_shortcut (*myfields); - if (!field || ( !mu_msg_field_xapian_value (field) && - !mu_msg_field_xapian_contact (field))) - len += printf ("%c", *myfields); - else - len += printf ("%s", - display_field(iter, field)); - ++myfields; - } - - if (len > 0) - g_print ("\n"); - - if (summary_len > 0) - print_summary (iter, summary_len); - - ++count; + if (mu_output_plain_row (iter, fields, summary_len)) + ++count; } while (mu_msg_iter_next (iter)); return count; } -/* create a linksdir if it not exist yet; if it already existed, - * remove old links if opts->clearlinks was specified */ -static gboolean -create_or_clear_linksdir_maybe (const char *linksdir, gboolean clearlinks) -{ - if (access (linksdir, F_OK) != 0) { - if (!mu_maildir_mkmdir (linksdir, 0700, TRUE)) - return FALSE; - - } else if (clearlinks) - mu_maildir_clear_links (linksdir); - - return TRUE; -} - static size_t make_links (MuMsgIter *iter, const char* linksdir, gboolean clearlinks) { size_t count = 0; - const MuMsgField *pathfield; - - if (!create_or_clear_linksdir_maybe (linksdir, clearlinks)) + + if (!mu_output_link_create_dir (linksdir, clearlinks)) return 0; if (mu_msg_iter_is_done (iter)) return 0; - pathfield = mu_msg_field_from_id (MU_MSG_FIELD_ID_PATH); - /* iterate over the found iters */ do { - const char *path; - - /* there's no data in the iter */ - if (mu_msg_iter_is_done (iter)) - return count; - - path = mu_msg_iter_get_field (iter, pathfield); - if (!path) - continue; - - /* this might happen if the database is not up-to-date */ - if (access (path, R_OK) != 0) { - g_warning ("Cannot read source message %s: %s", - path, strerror (errno)); - continue; - } - - if (!mu_maildir_link (path, linksdir)) - break; - ++count; + /* ignore errors...*/ + if (mu_output_link_row (iter, linksdir)) + ++count; } while (mu_msg_iter_next (iter)); diff --git a/src/mu-output-link.c b/src/mu-output-link.c new file mode 100644 index 00000000..4ec24cc9 --- /dev/null +++ b/src/mu-output-link.c @@ -0,0 +1,88 @@ +/* +** 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. +** +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /*HAVE_CONFIG_H*/ + +#include +#include +#include +#include +#include +#include "mu-msg.h" +#include "mu-maildir.h" +#include "mu-index.h" +#include "mu-msg-iter.h" +#include "mu-msg-str.h" + +#include "mu-util.h" +#include "mu-output-link.h" + + +/* create a linksdir if it not exist yet; if it already existed, + * remove old links if opts->clearlinks was specified */ +gboolean +mu_output_link_create_dir (const char *linksdir, gboolean clearlinks) +{ + g_return_val_if_fail (linksdir, FALSE); + + if (access (linksdir, F_OK) != 0) { + if (!mu_maildir_mkmdir (linksdir, 0700, TRUE)) + return FALSE; + + } else if (clearlinks) + mu_maildir_clear_links (linksdir); + + return TRUE; +} + +gboolean +mu_output_link_row (MuMsgIter *iter, const char* linksdir) +{ + const char *path; + const MuMsgField *pathfield; + + g_return_val_if_fail (iter, FALSE); + g_return_val_if_fail (linksdir, FALSE); + g_return_val_if_fail (!mu_msg_iter_is_done (iter), FALSE); + + pathfield = mu_msg_field_from_id (MU_MSG_FIELD_ID_PATH); + + path = mu_msg_iter_get_field (iter, pathfield); + if (!path) + return FALSE; + + /* this might happen if the database is not up-to-date, not an error */ + if (access (path, R_OK) != 0) { + if (errno == ENOENT) + g_warning ("Cannot find source message %s: " + "the database is not up-to-date", pathq); + else + g_warning ("Cannot read source message %s: %s", path, + strerror (errno)); + return FALSE; + } + + if (!mu_maildir_link (path, linksdir)) + return FALSE; + + return TRUE; +} + diff --git a/src/mu-output-link.h b/src/mu-output-link.h new file mode 100644 index 00000000..21ca591d --- /dev/null +++ b/src/mu-output-link.h @@ -0,0 +1,45 @@ +/* +** 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. +** +*/ + +#ifndef __MU_OUTPUT_LINK_H__ +#define __MU_OUTPUT_LINK_H__ + +/** + * create a target maildir to store the links if it does not exist yet + * + * @param linksdir path to the toplevel Maildir to create + * @param clearlinks if TRUE, clear any existing links in the target maildir + * + * @return TRUE if succeeded, FALSE otherwise + */ +gboolean mu_output_link_create_dir (const char *linksdir, gboolean clearlinks); + +/** + * create a symlink for for a message. the target dir should already + * exist, use mu_output_link_create_dir if needed. + * + * @param iter iterator pointing to a message row + * @param fields the fields to print (see MuMsgFields) + * @param summary_len number of lines to include in message summary + * + * @return TRUE if the printing succeeded, FALSE in case of error + */ +gboolean mu_output_link_row (MuMsgIter *iter, const char *linksdir); + +#endif /*__MU_OUTPUT_LINK_H__*/ diff --git a/src/mu-output-plain.c b/src/mu-output-plain.c new file mode 100644 index 00000000..0c011344 --- /dev/null +++ b/src/mu-output-plain.c @@ -0,0 +1,128 @@ +/* +** 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. +** +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /*HAVE_CONFIG_H*/ + +#include +#include +#include +#include + +#include "mu-msg.h" +#include "mu-maildir.h" +#include "mu-index.h" +#include "mu-msg-iter.h" +#include "mu-msg-str.h" + +/* #include "mu-util.h" */ +/* #include "mu-util-db.h" */ +/* #include "mu-cmd.h" */ +#include "mu-output-plain.h" + + +static const gchar* +display_field (MuMsgIter *iter, const MuMsgField* field) +{ + gint64 val; + + switch (mu_msg_field_type(field)) { + case MU_MSG_FIELD_TYPE_STRING: + return mu_msg_iter_get_field (iter, field); + + case MU_MSG_FIELD_TYPE_INT: + + if (mu_msg_field_id(field) == MU_MSG_FIELD_ID_PRIO) { + val = mu_msg_iter_get_field_numeric (iter, field); + return mu_msg_str_prio ((MuMsgPrio)val); + } + + if (mu_msg_field_id(field) == MU_MSG_FIELD_ID_FLAGS) { + val = mu_msg_iter_get_field_numeric (iter, field); + return mu_msg_str_flags_s ((MuMsgPrio)val); + } + + return mu_msg_iter_get_field (iter, field); /* as string */ + + case MU_MSG_FIELD_TYPE_TIME_T: + val = mu_msg_iter_get_field_numeric (iter, field); + return mu_msg_str_date_s ((time_t)val); + + case MU_MSG_FIELD_TYPE_BYTESIZE: + val = mu_msg_iter_get_field_numeric (iter, field); + return mu_msg_str_size_s ((time_t)val); + default: + g_return_val_if_reached (NULL); + } +} + + +static void +print_summary (MuMsgIter *iter, size_t summary_len) +{ + const char *summ; + MuMsg *msg; + + msg = mu_msg_iter_get_msg (iter); + if (!msg) { + g_warning ("%s: failed to create msg object", __FUNCTION__); + return; + } + + summ = mu_msg_get_summary (msg, summary_len); + g_print ("Summary: %s\n", summ ? summ : ""); + + mu_msg_destroy (msg); +} + + + +gboolean +mu_output_plain_row (MuMsgIter *iter, const char *fields, size_t summary_len) +{ + const char* myfields; + int len = 0; + + g_return_val_if_fail (iter, FALSE); + g_return_val_if_fail (fields, FALSE); + g_return_val_if_fail (!mu_msg_iter_is_done(iter), FALSE); + + myfields = fields; + while (*myfields) { + const MuMsgField* field; + field = mu_msg_field_from_shortcut (*myfields); + if (!field || ( !mu_msg_field_xapian_value (field) && + !mu_msg_field_xapian_contact (field))) + len += printf ("%c", *myfields); + else + len += printf ("%s", + display_field(iter, field)); + ++myfields; + } + + if (len > 0) + g_print ("\n"); + + if (summary_len > 0) + print_summary (iter, summary_len); + + return TRUE; +} + diff --git a/src/mu-output-plain.h b/src/mu-output-plain.h new file mode 100644 index 00000000..3d3abdab --- /dev/null +++ b/src/mu-output-plain.h @@ -0,0 +1,35 @@ +/* +** 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. +** +*/ + +#ifndef __MU_OUTPUT_PLAIN_H__ +#define __MU_OUTPUT_PLAIN_H__ + +/** + * print a row (message) to standard output + * + * @param iter iterator pointing to a message row + * @param fields the fields to print (see MuMsgFields) + * @param summary_len number of lines to include in message summary + * + * @return TRUE if the printing succeeded, FALSE in case of error + */ +gboolean mu_output_plain_row (MuMsgIter *iter, const char *fields, + size_t summary_len); + +#endif /*__MU_OUTPUT_PLAIN_H__*/