Logo Search packages:      
Sourcecode: pan version File versions

filter-info.cc

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Pan - A Newsreader for Gtk+
 * Copyright (C) 2002-2006  Charles Kerr <charles@rebelbase.com>
 *
 * 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; version 2 of the License.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <config.h>
extern "C" {
  #include <glib.h>
  #include <glib/gi18n.h>
}
#include <pan/general/foreach.h>
#include "filter-info.h"

using namespace pan;

/***
****
***/

void
FilterInfo :: clear ()
{
  _type = FilterInfo::TYPE_ERR;
  _ge = 0;
  _header.clear ();
  _text.clear ();
  _aggregates.clear ();
  _negate = false;
}

void
FilterInfo :: set_type_is (Type type) {
   clear ();
   _type = type;
}
void
FilterInfo :: set_type_ge (Type type, unsigned long ge) {
  clear ();
  _type = type;
  _ge = ge;
}
void
FilterInfo :: set_type_le (Type type, unsigned long le) {
  clear ();
  _type = type;
  _negate = true;
  _ge = le+1;  // le N == !ge N+1
}

void
FilterInfo :: set_type_aggregate_and () {
   clear ();
   _type = AGGREGATE_AND;
}
void
FilterInfo :: set_type_aggregate_or () {
   clear ();
   _type = AGGREGATE_OR;
}
void
FilterInfo :: set_type_text (const Quark                   & header,
                             const TextMatch::Description  & text)
{
  clear ();
  _type = TEXT;
  _header = header;
  _text.set (text);
}

/****
*****
****/

void
FilterInfo :: set_type_binary ()
{
   set_type_is (IS_BINARY);
}
void
FilterInfo :: set_type_byte_count_ge (unsigned long ge)
{
   set_type_ge (BYTE_COUNT_GE, ge);
}
void
FilterInfo :: set_type_cached ()
{
   set_type_is (IS_CACHED);
}
void
FilterInfo :: set_type_crosspost_count_ge (unsigned long ge)
{
   set_type_ge (CROSSPOST_COUNT_GE, ge);
}
void
FilterInfo :: set_type_days_old_ge (unsigned long ge)
{
   set_type_ge (DAYS_OLD_GE, ge);
}
void
FilterInfo :: set_type_days_old_le (unsigned long le)
{
   set_type_le (DAYS_OLD_GE, le);
}
void
FilterInfo :: set_type_line_count_ge (unsigned long ge)
{
   set_type_ge (LINE_COUNT_GE, ge);
}
void
FilterInfo :: set_type_score_ge (unsigned long ge)
{
   set_type_ge (SCORE_GE, ge);
}
void
FilterInfo :: set_type_score_le (unsigned long le)
{
   set_type_le (SCORE_GE, le);
}
void
FilterInfo :: set_type_is_unread ()
{
   set_type_is (IS_UNREAD);
}
void
FilterInfo :: set_type_posted_by_me ()
{
   set_type_is (IS_POSTED_BY_ME);
}

std::string
FilterInfo :: describe () const
{
  std::string ret;
  char buf[4096];

  if (_type==IS_BINARY && _negate)
  {
    ret = _("article doesn't have attachments");
  }
  else if (_type==IS_BINARY)
  {
    ret = _("the article has attachments");
  }
  else if (_type==IS_CACHED && _negate)
  {
    ret = _("the article isn't cached locally");
  }
  else if (_type==IS_CACHED)
  {
    ret = _("the article is cached locally");
  }
  else if (_type==IS_POSTED_BY_ME && _negate)
  {
    ret = _("the article wasn't posted by you");
  }
  else if (_type==IS_POSTED_BY_ME)
  {
    ret = _("the article was posted by you");
  }
  else if (_type==IS_UNREAD && _negate)
  {
    ret = _("the article has been read");
  }
  else if (_type==IS_UNREAD)
  {
    ret = _("the article hasn't been read");
  }
  else if (_type==BYTE_COUNT_GE && _negate)
  {
    g_snprintf (buf, sizeof(buf), _("the article is less than %ld bytes long"), _ge);
    ret = buf;
  }
  else if (_type==BYTE_COUNT_GE)
  {
    g_snprintf (buf, sizeof(buf), _("the article is at least %ld bytes long"), _ge);
    ret = buf;
  }
  else if (_type==LINE_COUNT_GE && _negate)
  {
    g_snprintf (buf, sizeof(buf), _("the article is less than %ld lines long"), _ge);
    ret = buf;
  }
  else if (_type==LINE_COUNT_GE)
  {
    g_snprintf (buf, sizeof(buf), _("the article is at least %ld lines long"), _ge);
    ret = buf;
  }
  else if (_type==DAYS_OLD_GE && _negate)
  {
    g_snprintf (buf, sizeof(buf), _("the article is less than %ld days old"), _ge);
    ret = buf;
  }
  else if (_type==DAYS_OLD_GE)
  {
    g_snprintf (buf, sizeof(buf), _("the article is at least %ld days old"), _ge);
    ret = buf;
  }
  else if (_type==CROSSPOST_COUNT_GE && _negate)
  {
    g_snprintf (buf, sizeof(buf), _("the article was posted to less than %ld groups"), _ge);
    ret = buf;
  }
  else if (_type==CROSSPOST_COUNT_GE)
  {
    g_snprintf (buf, sizeof(buf), _("the article was posted to at least %ld groups"), _ge);
    ret = buf;
  }
  else if (_type==SCORE_GE && _negate)
  {
    g_snprintf (buf, sizeof(buf), _("the article's score is less than %ld"), _ge);
    ret = buf;
  }
  else if (_type==SCORE_GE)
  {
    g_snprintf (buf, sizeof(buf), _("the article's score is %ld or higher"), _ge);
    ret = buf;
  }
  else if (_type==TEXT && _negate)
  {
#if 0
    const char * h (_header.c_str());
    const char * t (_text.get_state().text.c_str());
    switch (_text.get_state().type) {
      case TextMatch::CONTAINS:    g_snprintf (buf, sizeof(buf), _("%s doesn't contain \"%s\""), h, t); break;
      case TextMatch::IS:          g_snprintf (buf, sizeof(buf), _("%s isn't \"%s\""), h, t); break;
      case TextMatch::BEGINS_WITH: g_snprintf (buf, sizeof(buf), _("%s doesn't begin with \"%s\""), h, t); break;
      case TextMatch::ENDS_WITH:   g_snprintf (buf, sizeof(buf), _("%s doesn't end with \"%s\""), h, t); break;
      case TextMatch::REGEX:       g_snprintf (buf, sizeof(buf), _("%s doesn't match the regex \"%s\""), h, t); break;
    }
#else
    const char * h (_header.c_str());
    const char * t (_text._impl_text.c_str());
    switch (_text._impl_type) {
      case TextMatch::CONTAINS:    g_snprintf (buf, sizeof(buf), _("%s doesn't contain \"%s\""), h, t); break;
      case TextMatch::IS:          g_snprintf (buf, sizeof(buf), _("%s isn't \"%s\""), h, t); break;
      case TextMatch::BEGINS_WITH: g_snprintf (buf, sizeof(buf), _("%s doesn't begin with \"%s\""), h, t); break;
      case TextMatch::ENDS_WITH:   g_snprintf (buf, sizeof(buf), _("%s doesn't end with \"%s\""), h, t); break;
      case TextMatch::REGEX:       g_snprintf (buf, sizeof(buf), _("%s doesn't match the regex \"%s\""), h, t); break;
    }
#endif
    ret = buf;
  }
  else if (_type==TEXT)
  {
    const char * h (_header.c_str());
    //const char * t (_text.get_state().text.c_str());
    //switch (_text.get_state().type) {
    const char * t (_text._impl_text.c_str());
    switch (_text._impl_type) {
      case TextMatch::CONTAINS:    g_snprintf (buf, sizeof(buf), _("%s contains \"%s\""), h, t); break;
      case TextMatch::IS:          g_snprintf (buf, sizeof(buf), _("%s is \"%s\""), h, t); break;
      case TextMatch::BEGINS_WITH: g_snprintf (buf, sizeof(buf), _("%s begins with \"%s\""), h, t); break;
      case TextMatch::ENDS_WITH:   g_snprintf (buf, sizeof(buf), _("%s ends with \"%s\""), h, t); break;
      case TextMatch::REGEX:       g_snprintf (buf, sizeof(buf), _("%s matches the regex \"%s\""), h, t); break;
   }
    ret = buf;
  }
  else if (_type==AGGREGATE_AND && _negate)
  {
    ret = _("Any of these tests fail:");
    ret += "\n";
    foreach_const (aggregates_t, _aggregates, it)
      ret += "   " + it->describe() + "\n";
  }
  else if (_type==AGGREGATE_AND)
  {
    ret = _("All of these tests pass:");
    ret += "\n";
    foreach_const (aggregates_t, _aggregates, it)
      ret += "   " + it->describe() + "\n";
  }
  else if (_type==AGGREGATE_OR && _negate)
  {
    ret = _("None of these tests pass:");
    ret += "\n";
    foreach_const (aggregates_t, _aggregates, it)
      ret += "   " + it->describe() + "\n";
  }
  else if (_type==AGGREGATE_OR)
  {
    ret = _("Any of these tests pass:");
    ret += "\n";
    foreach_const (aggregates_t, _aggregates, it)
      ret += "   " + it->describe() + "\n";
  }

  return ret;
}

Generated by  Doxygen 1.6.0   Back to index