Changeset 13732


Ignore:
Timestamp:
Jan 1, 2013, 7:45:00 PM (8 years ago)
Author:
jordan
Message:

(gtk) #5207 'Simplify GTK+ client's category filter to match Qt and Web clients' -- done.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/filter.c

    r13731 r13732  
    3232/***
    3333****
    34 ****  CATEGORIES
     34****  TRACKERS
    3535****
    3636***/
     
    3838enum
    3939{
    40   CAT_FILTER_TYPE_ALL,
    41   CAT_FILTER_TYPE_PRIVATE,
    42   CAT_FILTER_TYPE_PUBLIC,
    43   CAT_FILTER_TYPE_HOST,
    44   CAT_FILTER_TYPE_PARENT,
    45   CAT_FILTER_TYPE_PRI_HIGH,
    46   CAT_FILTER_TYPE_PRI_NORMAL,
    47   CAT_FILTER_TYPE_PRI_LOW,
    48   CAT_FILTER_TYPE_TAG,
    49   CAT_FILTER_TYPE_SEPARATOR,
     40  TRACKER_FILTER_TYPE_ALL,
     41  TRACKER_FILTER_TYPE_HOST,
     42  TRACKER_FILTER_TYPE_SEPARATOR,
    5043};
    5144
    5245enum
    5346{
    54   CAT_FILTER_COL_NAME, /* human-readable name; ie, Legaltorrents */
    55   CAT_FILTER_COL_COUNT, /* how many matches there are */
    56   CAT_FILTER_COL_TYPE,
    57   CAT_FILTER_COL_HOST, /* pattern-matching text; ie, legaltorrents.com */
    58   CAT_FILTER_COL_PIXBUF,
    59   CAT_FILTER_N_COLS
     47  TRACKER_FILTER_COL_NAME, /* human-readable name; ie, Legaltorrents */
     48  TRACKER_FILTER_COL_COUNT, /* how many matches there are */
     49  TRACKER_FILTER_COL_TYPE,
     50  TRACKER_FILTER_COL_HOST, /* pattern-matching text; ie, legaltorrents.com */
     51  TRACKER_FILTER_COL_PIXBUF,
     52  TRACKER_FILTER_N_COLS
    6053};
    6154
     
    8679
    8780static void
    88 category_model_update_count (GtkTreeStore * store, GtkTreeIter * iter, int n)
     81tracker_model_update_count (GtkTreeStore * store, GtkTreeIter * iter, int n)
    8982{
    9083  int count;
    9184  GtkTreeModel * model = GTK_TREE_MODEL (store);
    92   gtk_tree_model_get (model, iter, CAT_FILTER_COL_COUNT, &count, -1);
     85  gtk_tree_model_get (model, iter, TRACKER_FILTER_COL_COUNT, &count, -1);
    9386  if (n != count)
    94     gtk_tree_store_set (store, iter, CAT_FILTER_COL_COUNT, n, -1);
     87    gtk_tree_store_set (store, iter, TRACKER_FILTER_COL_COUNT, n, -1);
    9588}
    9689
     
    108101      if (gtk_tree_model_get_iter (model, &iter, path))
    109102        gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
    110                             CAT_FILTER_COL_PIXBUF, pixbuf,
     103                            TRACKER_FILTER_COL_PIXBUF, pixbuf,
    111104                            -1);
    112105
     
    120113
    121114static gboolean
    122 category_filter_model_update (GtkTreeStore * store)
     115tracker_filter_model_update (GtkTreeStore * store)
    123116{
    124117  int i, n;
    125   int low = 0;
    126118  int all = 0;
    127   int high = 0;
    128   int public = 0;
    129   int normal = 0;
    130   int private = 0;
    131119  int store_pos;
    132   GtkTreeIter top;
    133120  GtkTreeIter iter;
    134121  GtkTreeModel * model = GTK_TREE_MODEL (store);
     
    138125  GObject * o = G_OBJECT (store);
    139126  GtkTreeModel * tmodel = GTK_TREE_MODEL (g_object_get_qdata (o, TORRENT_MODEL_KEY));
     127  const int first_tracker_pos = 2; /* offset past the "All" and the separator */
    140128
    141129  g_object_steal_qdata (o, DIRTY_KEY);
     
    191179
    192180      ++all;
    193 
    194       if (inf->isPrivate)
    195         ++private;
    196       else
    197         ++public;
    198 
    199       switch (tr_torrentGetPriority (tor))
    200         {
    201           case TR_PRI_HIGH: ++high; break;
    202           case TR_PRI_LOW: ++low; break;
    203           default: ++normal; break;
    204         }
    205181    }
    206182  while (gtk_tree_model_iter_next (tmodel, &iter));
     
    209185
    210186  /* update the "all" count */
    211   gtk_tree_model_iter_children (model, &top, NULL);
    212   category_model_update_count (store, &top, all);
    213 
    214   /* skip separator */
    215   gtk_tree_model_iter_next (model, &top);
    216 
    217   /* update the "hosts" subtree */
    218   gtk_tree_model_iter_next (model, &top);
    219   for (i=store_pos=0, n=hosts->len ; ;)
     187  gtk_tree_model_iter_children (model, &iter, NULL);
     188  tracker_model_update_count (store, &iter, all);
     189
     190  store_pos = first_tracker_pos;
     191  for (i=0, n=hosts->len ; ;)
    220192    {
    221193      const gboolean new_hosts_done = i >= n;
    222       const gboolean old_hosts_done = !gtk_tree_model_iter_nth_child (model, &iter, &top, store_pos);
     194      const gboolean old_hosts_done = !gtk_tree_model_iter_nth_child (model, &iter, NULL, store_pos);
    223195      gboolean remove_row = FALSE;
    224196      gboolean insert_row = FALSE;
     
    241213          int cmp;
    242214          char * host;
    243           gtk_tree_model_get (model, &iter, CAT_FILTER_COL_HOST, &host,  -1);
     215          gtk_tree_model_get (model, &iter, TRACKER_FILTER_COL_HOST, &host,  -1);
    244216          cmp = strcmp (host, hosts->pdata[i]);
    245217
     
    267239          char * name = get_name_from_host (host);
    268240          const int count = * (int*)g_hash_table_lookup (hosts_hash, host);
    269           gtk_tree_store_insert_with_values (store, &add, &top, store_pos,
    270                                              CAT_FILTER_COL_HOST, host,
    271                                              CAT_FILTER_COL_NAME, name,
    272                                              CAT_FILTER_COL_COUNT, count,
    273                                              CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_HOST,
     241          gtk_tree_store_insert_with_values (store, &add, NULL, store_pos,
     242                                             TRACKER_FILTER_COL_HOST, host,
     243                                             TRACKER_FILTER_COL_NAME, name,
     244                                             TRACKER_FILTER_COL_COUNT, count,
     245                                             TRACKER_FILTER_COL_TYPE, TRACKER_FILTER_TYPE_HOST,
    274246                                             -1);
    275247          path = gtk_tree_model_get_path (model, &add);
     
    285257          const char * host = hosts->pdata[i];
    286258          const int count = * (int*)g_hash_table_lookup (hosts_hash, host);
    287           category_model_update_count (store, &iter, count);
     259          tracker_model_update_count (store, &iter, count);
    288260          ++store_pos;
    289261          ++i;
    290262        }
    291263    }
    292 
    293   /* update the "public" subtree */
    294   gtk_tree_model_iter_next (model, &top);
    295   gtk_tree_model_iter_children (model, &iter, &top);
    296   category_model_update_count (store, &iter, public);
    297   gtk_tree_model_iter_next (model, &iter);
    298   category_model_update_count (store, &iter, private);
    299 
    300   /* update the "priority" subtree */
    301   gtk_tree_model_iter_next (model, &top);
    302   gtk_tree_model_iter_children (model, &iter, &top);
    303   category_model_update_count (store, &iter, high);
    304   gtk_tree_model_iter_next (model, &iter);
    305   category_model_update_count (store, &iter, normal);
    306   gtk_tree_model_iter_next (model, &iter);
    307   category_model_update_count (store, &iter, low);
    308264
    309265  /* cleanup */
     
    315271
    316272static GtkTreeModel *
    317 category_filter_model_new (GtkTreeModel * tmodel)
    318 {
    319   GtkTreeIter iter;
    320   const int invisible_number = -1; /* doesn't get rendered */
    321   GtkTreeStore * store = gtk_tree_store_new (CAT_FILTER_N_COLS,
     273tracker_filter_model_new (GtkTreeModel * tmodel)
     274{
     275  GtkTreeStore * store = gtk_tree_store_new (TRACKER_FILTER_N_COLS,
    322276                                             G_TYPE_STRING,
    323277                                             G_TYPE_INT,
     
    327281
    328282  gtk_tree_store_insert_with_values (store, NULL, NULL, -1,
    329                                      CAT_FILTER_COL_NAME, _("All"),
    330                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_ALL,
     283                                     TRACKER_FILTER_COL_NAME, _("All"),
     284                                     TRACKER_FILTER_COL_TYPE, TRACKER_FILTER_TYPE_ALL,
    331285                                     -1);
    332286  gtk_tree_store_insert_with_values (store, NULL, NULL, -1,
    333                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_SEPARATOR,
     287                                     TRACKER_FILTER_COL_TYPE, TRACKER_FILTER_TYPE_SEPARATOR,
    334288                                     -1);
    335289
    336   gtk_tree_store_insert_with_values (store, &iter, NULL, -1,
    337                                      CAT_FILTER_COL_NAME, _("Trackers"),
    338                                      CAT_FILTER_COL_COUNT, invisible_number,
    339                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PARENT,
    340                                      -1);
    341 
    342   gtk_tree_store_insert_with_values (store, &iter, NULL, -1,
    343                                      CAT_FILTER_COL_NAME, _("Privacy"),
    344                                      CAT_FILTER_COL_COUNT, invisible_number,
    345                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PARENT,
    346                                      -1);
    347   gtk_tree_store_insert_with_values (store, NULL, &iter, -1,
    348                                      CAT_FILTER_COL_NAME, _("Public"),
    349                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PUBLIC,
    350                                      -1);
    351   gtk_tree_store_insert_with_values (store, NULL, &iter, -1,
    352                                      CAT_FILTER_COL_NAME, _("Private"),
    353                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PRIVATE,
    354                                      -1);
    355 
    356   gtk_tree_store_insert_with_values (store, &iter, NULL, -1,
    357                                      CAT_FILTER_COL_NAME, _("Priority"),
    358                                      CAT_FILTER_COL_COUNT, invisible_number,
    359                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PARENT,
    360                                      -1);
    361   gtk_tree_store_insert_with_values (store, NULL, &iter, -1,
    362                                      CAT_FILTER_COL_NAME, _("High"),
    363                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PRI_HIGH,
    364                                      -1);
    365   gtk_tree_store_insert_with_values (store, NULL, &iter, -1,
    366                                      CAT_FILTER_COL_NAME, _("Normal"),
    367                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PRI_NORMAL,
    368                                      -1);
    369   gtk_tree_store_insert_with_values (store, NULL, &iter, -1,
    370                                      CAT_FILTER_COL_NAME, _("Low"),
    371                                      CAT_FILTER_COL_TYPE, CAT_FILTER_TYPE_PRI_LOW,
    372                                      -1);
    373 
    374290  g_object_set_qdata (G_OBJECT (store), TORRENT_MODEL_KEY, tmodel);
    375   category_filter_model_update (store);
     291  tracker_filter_model_update (store);
    376292  return GTK_TREE_MODEL (store);
    377293}
     
    381297{
    382298  int type;
    383   gtk_tree_model_get (m, iter, CAT_FILTER_COL_TYPE, &type, -1);
    384   return type == CAT_FILTER_TYPE_SEPARATOR;
    385 }
    386 
    387 static void
    388 category_model_update_idle (gpointer category_model)
    389 {
    390   GObject * o = G_OBJECT (category_model);
     299  gtk_tree_model_get (m, iter, TRACKER_FILTER_COL_TYPE, &type, -1);
     300  return type == TRACKER_FILTER_TYPE_SEPARATOR;
     301}
     302
     303static void
     304tracker_model_update_idle (gpointer tracker_model)
     305{
     306  GObject * o = G_OBJECT (tracker_model);
    391307  const gboolean pending = g_object_get_qdata (o, DIRTY_KEY) != NULL;
    392308  if (!pending)
    393309    {
    394       GSourceFunc func = (GSourceFunc) category_filter_model_update;
     310      GSourceFunc func = (GSourceFunc) tracker_filter_model_update;
    395311      g_object_set_qdata (o, DIRTY_KEY, GINT_TO_POINTER (1));
    396       gdk_threads_add_idle (func, category_model);
     312      gdk_threads_add_idle (func, tracker_model);
    397313    }
    398314}
     
    402318                           GtkTreePath   * path UNUSED,
    403319                           GtkTreeIter   * iter UNUSED,
    404                            gpointer        category_model)
    405 {
    406   category_model_update_idle (category_model);
     320                           gpointer        tracker_model)
     321{
     322  tracker_model_update_idle (tracker_model);
    407323}
    408324
     
    410326torrent_model_row_deleted_cb (GtkTreeModel * tmodel UNUSED,
    411327                              GtkTreePath  * path UNUSED,
    412                               gpointer       category_model)
    413 {
    414   category_model_update_idle (category_model);
     328                              gpointer       tracker_model)
     329{
     330  tracker_model_update_idle (tracker_model);
    415331}
    416332
     
    423339{
    424340  int type;
    425   int width = 0;
    426   const gboolean leaf = !gtk_tree_model_iter_has_child (tree_model, iter);
    427 
    428   gtk_tree_model_get (tree_model, iter, CAT_FILTER_COL_TYPE, &type, -1);
    429   if (type == CAT_FILTER_TYPE_HOST)
    430     width = 20;
    431 
    432   g_object_set (cell_renderer, "width", width,
    433                                "sensitive", leaf,
    434                                NULL);
    435 }
    436 
    437 static void
    438 is_capital_sensitive (GtkCellLayout   * cell_layout UNUSED,
    439                       GtkCellRenderer * cell_renderer,
    440                       GtkTreeModel    * tree_model,
    441                       GtkTreeIter     * iter,
    442                       gpointer          data UNUSED)
    443 {
    444   const gboolean leaf = !gtk_tree_model_iter_has_child (tree_model, iter);
    445 
    446   g_object_set (cell_renderer, "sensitive", leaf,
    447                                NULL);
     341  int width;
     342
     343  gtk_tree_model_get (tree_model, iter, TRACKER_FILTER_COL_TYPE, &type, -1);
     344  width = (type == TRACKER_FILTER_TYPE_HOST) ? 20 : 0;
     345  g_object_set (cell_renderer, "width", width, NULL);
    448346}
    449347
     
    457355  int count;
    458356  char buf[32];
    459   const gboolean leaf = !gtk_tree_model_iter_has_child (tree_model, iter);
    460 
    461   gtk_tree_model_get (tree_model, iter, CAT_FILTER_COL_COUNT, &count, -1);
     357
     358  gtk_tree_model_get (tree_model, iter, TRACKER_FILTER_COL_COUNT, &count, -1);
    462359
    463360  if (count >= 0)
     
    467364
    468365  g_object_set (cell_renderer, "text", buf,
    469                                "sensitive", leaf,
    470366                               NULL);
    471367}
     
    493389
    494390static GtkWidget *
    495 category_combo_box_new (GtkTreeModel * tmodel)
     391tracker_combo_box_new (GtkTreeModel * tmodel)
    496392{
    497393  GtkWidget * c;
     
    501397  GtkComboBox * c_combo_box;
    502398
    503   /* create the category combobox */
    504   cat_model = category_filter_model_new (tmodel);
     399  /* create the tracker combobox */
     400  cat_model = tracker_filter_model_new (tmodel);
    505401  c = gtk_combo_box_new_with_model (cat_model);
    506402  c_combo_box = GTK_COMBO_BOX (c);
     
    516412                                      render_pixbuf_func, NULL, NULL);
    517413  gtk_cell_layout_set_attributes (c_cell_layout, r,
    518                                   "pixbuf", CAT_FILTER_COL_PIXBUF,
     414                                  "pixbuf", TRACKER_FILTER_COL_PIXBUF,
    519415                                  NULL);
    520416
     
    522418  gtk_cell_layout_pack_start (c_cell_layout, r, FALSE);
    523419  gtk_cell_layout_set_attributes (c_cell_layout, r,
    524                                   "text", CAT_FILTER_COL_NAME,
     420                                  "text", TRACKER_FILTER_COL_NAME,
    525421                                  NULL);
    526   gtk_cell_layout_set_cell_data_func (c_cell_layout, r,
    527                                       is_capital_sensitive,
    528                                       NULL, NULL);
    529 
    530422
    531423  r = number_renderer_new ();
     
    543435
    544436static gboolean
    545 test_category (tr_torrent * tor, int active_category_type, const char * host)
    546 {
    547   const tr_info * const inf = tr_torrentInfo (tor);
    548 
    549   switch (active_category_type)
    550     {
    551       case CAT_FILTER_TYPE_ALL:
    552         return TRUE;
    553 
    554       case CAT_FILTER_TYPE_PRIVATE:
    555         return inf->isPrivate;
    556 
    557       case CAT_FILTER_TYPE_PUBLIC:
    558         return !inf->isPrivate;
    559 
    560       case CAT_FILTER_TYPE_PRI_HIGH:
    561         return tr_torrentGetPriority (tor) == TR_PRI_HIGH;
    562 
    563       case CAT_FILTER_TYPE_PRI_NORMAL:
    564         return tr_torrentGetPriority (tor) == TR_PRI_NORMAL;
    565 
    566       case CAT_FILTER_TYPE_PRI_LOW:
    567         return tr_torrentGetPriority (tor) == TR_PRI_LOW;
    568 
    569       case CAT_FILTER_TYPE_HOST:
    570         {
    571           unsigned int i;
    572           char tmp[1024];
    573           for (i=0; i<inf->trackerCount; ++i)
    574             {
    575               gtr_get_host_from_url (tmp, sizeof (tmp), inf->trackers[i].announce);
    576               if (!strcmp (tmp, host))
    577                 break;
    578             }
    579           return i < inf->trackerCount;
    580         }
    581 
    582       case CAT_FILTER_TYPE_TAG:
    583         /* FIXME */
    584         return TRUE;
    585 
    586       default:
    587         return TRUE;
    588     }
     437test_tracker (tr_torrent * tor, int active_tracker_type, const char * host)
     438{
     439  gboolean matches = TRUE;
     440
     441  if (active_tracker_type == TRACKER_FILTER_TYPE_HOST)
     442    {
     443      unsigned int i;
     444      char tmp[1024];
     445      const tr_info * const inf = tr_torrentInfo (tor);
     446
     447      for (i=0; i<inf->trackerCount; ++i)
     448        {
     449          gtr_get_host_from_url (tmp, sizeof (tmp), inf->trackers[i].announce);
     450          if (!strcmp (tmp, host))
     451            break;
     452        }
     453
     454      matches = i < inf->trackerCount;
     455    }
     456
     457  return matches;
    589458}
    590459
     
    760629  int width;
    761630  int ypad;
    762   const gboolean leaf = !gtk_tree_model_iter_has_child (tree_model, iter);
    763631
    764632  gtk_tree_model_get (tree_model, iter, ACTIVITY_FILTER_COL_TYPE, &type, -1);
     
    767635
    768636  g_object_set (cell_renderer, "width", width,
    769                                "sensitive", leaf,
    770637                               "ypad", ypad,
    771638                               NULL);
     
    923790{
    924791  GtkWidget * activity;
    925   GtkWidget * category;
     792  GtkWidget * tracker;
    926793  GtkWidget * entry;
    927794  GtkTreeModel * filter_model;
    928795  int active_activity_type;
    929   int active_category_type;
    930   char * active_category_host;
     796  int active_tracker_type;
     797  char * active_tracker_host;
    931798};
    932799
     
    943810  text = (const char*) g_object_get_qdata (o, TEXT_KEY);
    944811
    945   return (tor != NULL) && test_category (tor, data->active_category_type, data->active_category_host)
     812  return (tor != NULL) && test_tracker (tor, data->active_tracker_type, data->active_tracker_host)
    946813                       && test_torrent_activity (tor, data->active_activity_type)
    947814                       && testText (tor, text);
     
    966833  data->active_activity_type = type;
    967834
    968   /* set the active category type & host from the category combobox */
    969   combo = GTK_COMBO_BOX (data->category);
     835  /* set the active tracker type & host from the tracker combobox */
     836  combo = GTK_COMBO_BOX (data->tracker);
    970837  model = gtk_combo_box_get_model (combo);
    971838  if (gtk_combo_box_get_active_iter (combo, &iter))
    972839    {
    973       gtk_tree_model_get (model, &iter, CAT_FILTER_COL_TYPE, &type,
    974                                         CAT_FILTER_COL_HOST, &host,
     840      gtk_tree_model_get (model, &iter, TRACKER_FILTER_COL_TYPE, &type,
     841                                        TRACKER_FILTER_COL_HOST, &host,
    975842                                        -1);
    976843    }
    977844  else
    978845    {
    979       type = CAT_FILTER_TYPE_ALL;
     846      type = TRACKER_FILTER_TYPE_ALL;
    980847      host = NULL;
    981848    }
    982   g_free (data->active_category_host);
    983   data->active_category_host = host;
    984   data->active_category_type = type;
     849  g_free (data->active_tracker_host);
     850  data->active_tracker_host = host;
     851  data->active_tracker_type = type;
    985852
    986853  /* refilter */
     
    996863  GtkWidget * s;
    997864  GtkWidget * activity;
    998   GtkWidget * category;
     865  GtkWidget * tracker;
    999866  GtkBox * h_box;
    1000867  const char * str;
     
    1009876  data = g_new0 (struct filter_data, 1);
    1010877  data->activity = activity = activity_combo_box_new (tmodel);
    1011   data->category = category = category_combo_box_new (tmodel);
     878  data->tracker = tracker = tracker_combo_box_new (tmodel);
    1012879  data->filter_model = gtk_tree_model_filter_new (tmodel, NULL);
    1013880
    1014   g_object_set (G_OBJECT (data->category), "width-request", 170, NULL);
    1015   g_object_set_qdata (G_OBJECT (gtk_combo_box_get_model (GTK_COMBO_BOX (data->category))), SESSION_KEY, session);
     881  g_object_set (G_OBJECT (data->tracker), "width-request", 170, NULL);
     882  g_object_set_qdata (G_OBJECT (gtk_combo_box_get_model (GTK_COMBO_BOX (data->tracker))), SESSION_KEY, session);
    1016883
    1017884  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (data->filter_model),
    1018885                                          is_row_visible, data, g_free);
    1019886
    1020   g_signal_connect (data->category, "changed", G_CALLBACK (selection_changed_cb), data);
     887  g_signal_connect (data->tracker, "changed", G_CALLBACK (selection_changed_cb), data);
    1021888  g_signal_connect (data->activity, "changed", G_CALLBACK (selection_changed_cb), data);
    1022889
     
    1039906  gtk_box_pack_start (h_box, w, FALSE, FALSE, 0);
    1040907
    1041   /* add the category combobox */
    1042   w = category;
     908  /* add the tracker combobox */
     909  w = tracker;
    1043910  gtk_box_pack_start (h_box, w, TRUE, TRUE, 0);
    1044911
Note: See TracChangeset for help on using the changeset viewer.