Changeset 1042

Show
Ignore:
Timestamp:
03/16/08 09:55:40 (4 years ago)
Author:
rakshasa
Message:

* Changed view_filter to use boolean commands instead of custom
functors.

Location:
trunk/rtorrent/src
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • trunk/rtorrent/src/command_ui.cc

    r1039 r1042  
    5454 
    5555typedef void (core::ViewManager::*view_filter_slot)(const std::string&, const core::ViewManager::sort_args&); 
     56typedef void (core::ViewManager::*view_cfilter_slot)(const std::string&, const std::string&); 
    5657 
    5758torrent::Object 
     
    7879 
    7980torrent::Object 
     81apply_view_cfilter(view_cfilter_slot viewFilterSlot, const torrent::Object& rawArgs) { 
     82  const torrent::Object::list_type& args = rawArgs.as_list(); 
     83 
     84  if (args.size() != 2) 
     85    throw torrent::input_error("Too few arguments."); 
     86 
     87  const std::string& name = args.front().as_string(); 
     88   
     89  if (name.empty()) 
     90    throw torrent::input_error("First argument must be a string."); 
     91 
     92  (control->view_manager()->*viewFilterSlot)(name, args.back().as_string()); 
     93 
     94  return torrent::Object(); 
     95} 
     96 
     97torrent::Object 
    8098apply_view_sort(const torrent::Object& rawArgs) { 
    8199  const torrent::Object::list_type& args = rawArgs.as_list(); 
     
    143161  rpc::print_object_std(&result, &rawArgs, 0); 
    144162  return result; 
     163} 
     164 
     165bool 
     166as_boolean(const torrent::Object& rawArgs) { 
     167  switch (rawArgs.type()) { 
     168  case torrent::Object::TYPE_VALUE:  return rawArgs.as_value(); 
     169  case torrent::Object::TYPE_STRING: return !rawArgs.as_string().empty(); 
     170  case torrent::Object::TYPE_LIST:   return !rawArgs.as_list().empty(); 
     171  case torrent::Object::TYPE_MAP:    return !rawArgs.as_map().empty(); 
     172  default: return false; 
     173  } 
     174} 
     175 
     176torrent::Object 
     177apply_not(rpc::target_type target, const torrent::Object& rawArgs) { 
     178  return (int64_t)as_boolean(rawArgs); 
     179} 
     180 
     181torrent::Object 
     182apply_and(rpc::target_type target, const torrent::Object& rawArgs) { 
     183  if (rawArgs.type() != torrent::Object::TYPE_LIST) 
     184    return as_boolean(rawArgs); 
     185 
     186  for (torrent::Object::list_const_iterator itr = rawArgs.as_list().begin(), last = rawArgs.as_list().end(); itr != last; itr++) 
     187    if (!as_boolean(rpc::parse_command_single(target, itr->as_string()))) 
     188      return (int64_t)false; 
     189 
     190  return (int64_t)true; 
     191} 
     192 
     193torrent::Object 
     194apply_or(rpc::target_type target, const torrent::Object& rawArgs) { 
     195  if (rawArgs.type() != torrent::Object::TYPE_LIST) 
     196    return as_boolean(rawArgs); 
     197 
     198  for (torrent::Object::list_const_iterator itr = rawArgs.as_list().begin(), last = rawArgs.as_list().end(); itr != last; itr++) 
     199    if (as_boolean(rpc::parse_command_single(target, itr->as_string()))) 
     200      return (int64_t)true; 
     201 
     202  return (int64_t)false; 
    145203} 
    146204 
     
    288346  ADD_COMMAND_NONE_L("view_set",        rak::ptr_fn(&apply_view_set)); 
    289347 
    290   ADD_COMMAND_LIST("view_filter",       rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_filter)); 
     348  ADD_COMMAND_LIST("view_filter",       rak::bind_ptr_fn(&apply_view_cfilter, &core::ViewManager::set_filter)); 
    291349  ADD_COMMAND_LIST("view_filter_on",    rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_filter_on)); 
    292350 
     
    302360  ADD_ANY_NONE("cat",                   rak::ptr_fn(&apply_cat)); 
    303361  ADD_ANY_NONE("if",                    rak::bind_ptr_fn(&apply_if, 0)); 
     362  ADD_ANY_NONE("not",                   rak::ptr_fn(&apply_not)); 
     363  ADD_ANY_NONE("and",                   rak::ptr_fn(&apply_and)); 
     364  ADD_ANY_NONE("or",                    rak::ptr_fn(&apply_or)); 
    304365 
    305366  // A temporary command for handling stuff until we get proper 
  • trunk/rtorrent/src/core/view.cc

    r992 r1042  
    4040#include <functional> 
    4141#include <rak/functional.h> 
     42#include <rpc/parse_commands.h> 
    4243#include <sigc++/adaptors/bind.h> 
    4344#include <torrent/download.h> 
     
    129130 
    130131struct view_downloads_filter : std::unary_function<Download*, bool> { 
    131   view_downloads_filter(const View::filter_list& s) : m_filter(s) {} 
     132  view_downloads_filter(const std::string& cmd) : m_command(cmd) {} 
    132133 
    133134  bool operator () (Download* d1) const { 
    134     for (View::filter_list::const_iterator itr = m_filter.begin(), last = m_filter.end(); itr != last; ++itr) 
    135       if (!(**itr)(d1)) 
    136         return false; 
    137  
    138     // The default filter action is to return true, to not filter the 
    139     // download out. 
    140     return true; 
     135    try { 
     136      torrent::Object result = rpc::parse_command_single(rpc::make_target(d1), m_command); 
     137 
     138      switch (result.type()) { 
     139      case torrent::Object::TYPE_NONE:   return false; 
     140      case torrent::Object::TYPE_VALUE:  return result.as_value(); 
     141      case torrent::Object::TYPE_STRING: return !result.as_string().empty(); 
     142      case torrent::Object::TYPE_LIST:   return !result.as_list().empty(); 
     143      case torrent::Object::TYPE_MAP:    return !result.as_map().empty(); 
     144      } 
     145 
     146      // The default filter action is to return true, to not filter 
     147      // the download out. 
     148      return true; 
     149 
     150    } catch (torrent::input_error& e) { 
     151      return false; 
     152    } 
    141153  } 
    142154 
    143   const View::filter_list& m_filter; 
     155  const std::string&       m_command; 
    144156}; 
    145157 
     
    233245      // Erase even if it is in visible so that the download is 
    234246      // re-sorted. 
     247      // 
     248      // Do we really want to do this? 
    235249      erase(itr); 
    236250      insert_visible(download); 
  • trunk/rtorrent/src/core/view.h

    r1041 r1042  
    6363class DownloadList; 
    6464class ViewSort; 
    65 class ViewFilter; 
    6665 
    6766class View : private std::vector<Download*> { 
     
    7069  typedef sigc::signal0<void>            signal_type; 
    7170  typedef std::vector<const ViewSort*>   sort_list; 
    72   typedef std::vector<const ViewFilter*> filter_list; 
    7371 
    7472  using base_type::iterator; 
     
    118116  void                filter(); 
    119117 
    120   void                set_filter(const filter_list& s)        { m_filter = s; } 
     118  void                set_filter(const std::string& s)        { m_filter = s; } 
    121119  void                set_filter_on(int event); 
    122120 
    123   void                set_cfilter(const std::string& s)        { m_cfilter = s; } 
    124121 
    125122  void                clear_filter_on(); 
     
    163160  sort_list           m_sortCurrent; 
    164161 
    165   filter_list         m_filter; 
    166  
    167   std::string         m_cfilter; 
     162  // This should be replaced by a faster non-string command type. 
     163  std::string         m_filter; 
    168164 
    169165  rak::timer          m_lastChanged; 
     
    178174}; 
    179175 
    180 class ViewFilter { 
    181 public: 
    182   virtual ~ViewFilter() {} 
    183  
    184   virtual bool operator () (Download* d1) const = 0; 
    185 }; 
    186  
    187176} 
    188177 
  • trunk/rtorrent/src/core/view_manager.cc

    r966 r1042  
    112112}; 
    113113 
    114 class ViewFilterVariableValue : public ViewFilter { 
    115 public: 
    116   ViewFilterVariableValue(const char* name, torrent::Object::value_type v, bool inverse = false) : 
    117     m_name(name), m_value(v), m_inverse(inverse) {} 
    118  
    119   virtual bool operator () (Download* d1) const { 
    120     return (rpc::call_command_value(m_name, rpc::make_target(d1)) == m_value) != m_inverse; 
    121   } 
    122  
    123 private: 
    124   const char*                 m_name; 
    125   torrent::Object::value_type m_value; 
    126   bool                        m_inverse; 
    127 }; 
    128  
    129114// Really need to implement a factory and allow options in the sort 
    130115// statements. 
     
    144129  m_sort["state_changed"]         = new ViewSortVariableValue("d.get_state_changed"); 
    145130  m_sort["state_changed_reverse"] = new ViewSortVariableValue("d.get_state_changed", true); 
    146  
    147   m_filter["started"]     = new ViewFilterVariableValue("d.get_state", 1); 
    148   m_filter["stopped"]     = new ViewFilterVariableValue("d.get_state", 0); 
    149   m_filter["complete"]    = new ViewFilterVariableValue("d.get_complete", 0, true); 
    150   m_filter["incomplete"]  = new ViewFilterVariableValue("d.get_complete", 0); 
    151   m_filter["hashing"]     = new ViewFilterVariableValue("d.get_hashing", 0, true); 
    152131} 
    153132 
     
    156135  std::for_each(begin(), end(), rak::call_delete<View>()); 
    157136  std::for_each(m_sort.begin(), m_sort.end(), rak::on(rak::mem_ref(&sort_map::value_type::second), rak::call_delete<ViewSort>())); 
    158   std::for_each(m_filter.begin(), m_filter.end(), rak::on(rak::mem_ref(&filter_map::value_type::second), rak::call_delete<ViewFilter>())); 
    159137 
    160138  base_type::clear(); 
     
    231209} 
    232210 
    233 inline ViewManager::filter_list 
    234 ViewManager::build_filter_list(const filter_args& args) { 
    235   View::filter_list filterList; 
    236   filterList.reserve(args.size()); 
    237  
    238   for (filter_args::const_iterator itr = args.begin(), last = args.end(); itr != last; ++itr) { 
    239     filter_map::const_iterator filterItr = m_filter.find(itr->c_str()); 
    240  
    241     if (filterItr == m_filter.end()) 
    242       throw torrent::input_error("Invalid filtering identifier."); 
    243  
    244     filterList.push_back(filterItr->second); 
    245   } 
    246  
    247   return filterList; 
    248 } 
    249  
    250 void 
    251 ViewManager::set_filter(const std::string& name, const filter_args& args) { 
    252   iterator viewItr = find_throw(name); 
    253  
    254   (*viewItr)->set_filter(build_filter_list(args)); 
     211void 
     212ViewManager::set_filter(const std::string& name, const std::string& cmd) { 
     213  iterator viewItr = find_throw(name); 
     214 
     215  (*viewItr)->set_filter(cmd); 
    255216  (*viewItr)->filter(); 
    256217} 
  • trunk/rtorrent/src/core/view_manager.h

    r938 r1042  
    6161  typedef std::list<std::string>                sort_args; 
    6262   
    63   typedef std::map<const char*, ViewFilter*, view_manager_comp> filter_map; 
    64   typedef View::filter_list                     filter_list; 
    6563  typedef std::list<std::string>                filter_args; 
    6664   
     
    106104  void                set_sort_current(const std::string& name, const sort_args& sort); 
    107105 
    108   void                set_filter(const std::string& name, const filter_args& args); 
     106  void                set_filter(const std::string& name, const std::string& cmd); 
    109107  void                set_filter_on(const std::string& name, const filter_args& args); 
    110108 
    111109private: 
    112110  inline sort_list    build_sort_list(const sort_args& args); 
    113   inline filter_list  build_filter_list(const sort_args& args); 
    114111 
    115112  DownloadList*       m_list; 
    116113 
    117114  sort_map            m_sort; 
    118   filter_map          m_filter; 
    119115}; 
    120116 
  • trunk/rtorrent/src/main.cc

    r1025 r1042  
    185185 
    186186       "view_add = started\n" 
    187        "view_filter = started,started\n" 
     187       "view_filter = started,d.get_state=\n" 
    188188       "view_filter_on = started,start,stop\n" 
    189189       "view_sort_new = started,name\n" 
     
    191191 
    192192       "view_add = stopped\n" 
    193        "view_filter = stopped,stopped\n" 
     193       "view_filter = stopped,not=$d.get_state=\n" 
    194194       "view_filter_on = stopped,start,stop\n" 
    195195       "view_sort_new = stopped,name\n" 
     
    197197 
    198198       "view_add = complete\n" 
    199        "view_filter = complete,complete\n" 
     199       "view_filter = complete,d.get_complete=\n" 
    200200       "view_filter_on = complete,hash_done,finished\n" 
    201201       "view_sort_new = complete,state_changed\n" 
     
    203203 
    204204       "view_add = incomplete\n" 
    205        "view_filter = incomplete,incomplete\n" 
     205       "view_filter = incomplete,not=$d.get_complete=\n" 
    206206       "view_filter_on = incomplete,hash_done,finished\n" 
    207207       "view_sort_new = incomplete,state_changed\n" 
     
    210210       // The hashing view does not include stopped torrents. 
    211211       "view_add = hashing\n" 
    212        "view_filter = hashing,hashing\n" 
     212       "view_filter = hashing,d.get_hashing=\n" 
    213213       "view_filter_on = hashing,hash_queued,hash_removed,hash_done\n" 
    214214       "view_sort_new = hashing,state_changed\n" 
     
    216216 
    217217       "view_add = seeding\n" 
    218        "view_filter = seeding,started,complete\n" 
     218       "view_filter = seeding,\"and=d.get_state=,d.get_complete=\"\n" 
    219219       "view_filter_on = seeding,start,stop\n" 
    220220       "view_sort_new = seeding,state_changed\n" 
  • trunk/rtorrent/src/rpc/command.h

    r1041 r1042  
    103103  typedef const torrent::Object (*tracker_slot)  (Command*, torrent::Tracker*, const torrent::Object&); 
    104104 
     105  typedef const torrent::Object (*download_pair_slot) (Command*, core::Download*, core::Download*, const torrent::Object&); 
     106 
    105107  static const int target_generic  = 0; 
    106108  static const int target_any      = 1; 
     
    110112  static const int target_file     = 5; 
    111113  static const int target_file_itr = 6; 
     114 
     115  static const int target_download_pair = 7; 
    112116 
    113117  Command() {} 
     
    119123}; 
    120124 
    121 template <typename T> 
     125template <typename T1 = void, typename T2 = void> 
    122126struct target_type_id { 
    123127  // Nothing here, so we cause an error. 
    124128}; 
    125129 
    126 template <> struct target_type_id<Command::generic_slot>  { static const int value = Command::target_generic; }; 
    127 template <> struct target_type_id<Command::any_slot>      { static const int value = Command::target_any; }; 
    128 template <> struct target_type_id<Command::download_slot> { static const int value = Command::target_download; }; 
    129 template <> struct target_type_id<Command::peer_slot>     { static const int value = Command::target_peer; }; 
    130 template <> struct target_type_id<Command::tracker_slot>  { static const int value = Command::target_tracker; }; 
    131 template <> struct target_type_id<Command::file_slot>     { static const int value = Command::target_file; }; 
    132 template <> struct target_type_id<Command::file_itr_slot> { static const int value = Command::target_file_itr; }; 
     130template <> struct target_type_id<Command::generic_slot>       { static const int value = Command::target_generic; }; 
     131template <> struct target_type_id<Command::any_slot>           { static const int value = Command::target_any; }; 
     132template <> struct target_type_id<Command::download_slot>      { static const int value = Command::target_download; }; 
     133template <> struct target_type_id<Command::peer_slot>          { static const int value = Command::target_peer; }; 
     134template <> struct target_type_id<Command::tracker_slot>       { static const int value = Command::target_tracker; }; 
     135template <> struct target_type_id<Command::file_slot>          { static const int value = Command::target_file; }; 
     136template <> struct target_type_id<Command::file_itr_slot>      { static const int value = Command::target_file_itr; }; 
     137 
     138template <> struct target_type_id<Command::download_pair_slot> { static const int value = Command::target_download_pair; }; 
     139 
     140template <> struct target_type_id<>                            { static const int value = Command::target_generic; }; 
     141template <> struct target_type_id<target_type>                 { static const int value = Command::target_any; }; 
     142template <> struct target_type_id<core::Download*>             { static const int value = Command::target_download; }; 
     143template <> struct target_type_id<torrent::Peer*>              { static const int value = Command::target_peer; }; 
     144template <> struct target_type_id<torrent::Tracker*>           { static const int value = Command::target_tracker; }; 
     145template <> struct target_type_id<torrent::File*>              { static const int value = Command::target_file; }; 
     146template <> struct target_type_id<torrent::FileListIterator*>  { static const int value = Command::target_file_itr; }; 
     147 
     148template <> struct target_type_id<core::Download*, core::Download*> { static const int value = Command::target_download_pair; }; 
    133149 
    134150} 
  • trunk/rtorrent/src/rpc/command_map.h

    r1041 r1042  
    7373    Command::peer_slot     m_peerSlot; 
    7474    Command::tracker_slot  m_trackerSlot; 
     75 
     76    Command::download_slot m_downloadPairSlot; 
    7577  }; 
    7678 
     
    109111  iterator            insert(key_type key, Command* variable, int flags, const char* parm, const char* doc); 
    110112 
     113  // Make this a wrapper call to insert without extra fluff. 
    111114  template <typename T> 
    112115  void                insert_type(key_type key, Command* variable, T targetSlot, int flags, const char* parm, const char* doc) { 
     
    133136 
    134137inline target_type make_target()                                  { return target_type((int)Command::target_generic, NULL); } 
    135 inline target_type make_target(core::Download* target)            { return target_type((int)Command::target_download, target); } 
    136 inline target_type make_target(torrent::Peer* target)             { return target_type((int)Command::target_peer, target); } 
    137 inline target_type make_target(torrent::Tracker* target)          { return target_type((int)Command::target_tracker, target); } 
    138 inline target_type make_target(torrent::File* target)             { return target_type((int)Command::target_file, target); } 
    139 inline target_type make_target(torrent::FileListIterator* target) { return target_type((int)Command::target_file_itr, target); } 
    140138inline target_type make_target(int type, void* target)            { return target_type(type, target); } 
     139inline target_type make_target(int type, void* target1, void* target2) { return target_type(type, target1, target2); } 
     140 
     141template <typename T> 
     142inline target_type make_target(T target) { 
     143  return target_type((int)target_type_id<T>::value, target); 
     144} 
    141145 
    142146} 
  • trunk/rtorrent/src/rpc/parse_commands.h

    r1026 r1042  
    7272const char*            parse_command_name(const char* first, const char* last, std::string* dest); 
    7373 
     74inline torrent::Object 
     75parse_command_single(target_type target, const std::string& cmd) { 
     76  return parse_command(target, cmd.c_str(), cmd.c_str() + cmd.size()).first; 
     77} 
     78 
    7479inline void 
    7580parse_command_single_std(const std::string& cmd) {