~ubuntu-branches/ubuntu/karmic/tasks/karmic

« back to all changes in this revision

Viewing changes to libkoto/koto-group-store.c

  • Committer: Bazaar Package Importer
  • Author(s): Ross Burton
  • Date: 2007-05-27 18:52:30 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070527185230-2fzwlk3fkoftp8ac
Tags: 0.7-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include <config.h>
20
20
#include <glib/gi18n.h>
21
21
#include <libecal/e-cal-view.h>
 
22
#include <libedataserver/e-data-server-util.h>
22
23
 
23
24
#include "koto-group-store.h"
24
25
 
310
311
koto_group_store_add_new_group (KotoGroupStore *store, GtkTreeIter *iter, const char *name)
311
312
{
312
313
  KotoGroupStorePrivate *priv;
313
 
  GtkTreeIter *it;
 
314
  GtkTreeIter *current_it, new_it;
314
315
  g_return_if_fail (KOTO_IS_GROUP_STORE (store));
315
316
  g_return_if_fail (name);
316
317
  
317
318
  priv = GET_PRIVATE (store);
318
319
  
319
 
  it = g_hash_table_lookup (priv->iter_hash, name);
320
 
  if (it) {
321
 
    *iter = *it;
 
320
  current_it = g_hash_table_lookup (priv->iter_hash, name);
 
321
  if (current_it) {
 
322
    if (iter) *iter = *current_it;
322
323
  } else {
323
324
    gtk_list_store_insert_with_values (GTK_LIST_STORE (store),
324
 
                                       iter, 0,
 
325
                                       &new_it, 0,
325
326
                                       COL_TYPE, GROUP_TYPE_CATEGORY,
326
327
                                       COL_NAME, name,
327
328
                                       -1);
328
 
    g_hash_table_insert (priv->iter_hash, g_strdup (name), gtk_tree_iter_copy (iter));
329
 
  }
330
 
}
 
329
    g_hash_table_insert (priv->iter_hash, g_strdup (name), gtk_tree_iter_copy (&new_it));
 
330
 
 
331
    if (iter) *iter = new_it;
 
332
  }
 
333
}
 
334
 
 
335
/* Helper struct for koto_group_store_match_group */
 
336
typedef struct {
 
337
  const char *guess;
 
338
  char *found;
 
339
} MatchData;
 
340
 
 
341
/* Helper function for koto_group_store_match_group */
 
342
static gboolean
 
343
find_group (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
 
344
{
 
345
  MatchData *data = user_data;
 
346
  KotoGroupType type;
 
347
  char *name = NULL;
 
348
 
 
349
  gtk_tree_model_get (model, iter,
 
350
                      COL_TYPE, &type,
 
351
                      COL_NAME, &name,
 
352
                      -1);
 
353
  
 
354
  if (type != GROUP_TYPE_CATEGORY) {
 
355
    g_free (name);
 
356
    return FALSE;
 
357
  }
 
358
 
 
359
  if (e_util_utf8_strstrcasedecomp (name, data->guess)) {
 
360
    data->found = name;
 
361
    return TRUE;
 
362
  } else {
 
363
    g_free (name);
 
364
    return FALSE;
 
365
  }
 
366
}
 
367
 
 
368
/**
 
369
 * koto_group_store_match_group:
 
370
 * @store: A #KotoGroupStore.
 
371
 * @guess: The group name to search for.
 
372
 *
 
373
 * Seach the groups for a group named roughly @guess.  If one is found, return
 
374
 * it as a newly allocated string, otherwise return NULL.
 
375
 *
 
376
 * Currently "named roughly" means case-insensitive stripped decomposed
 
377
 * substrings.
 
378
 */
 
379
char *
 
380
koto_group_store_match_group (KotoGroupStore *store, const char *guess)
 
381
{
 
382
  MatchData data;
 
383
 
 
384
  g_return_val_if_fail (KOTO_IS_GROUP_STORE (store), NULL);
 
385
  g_return_val_if_fail (guess, NULL);
 
386
 
 
387
  data.guess = guess;
 
388
  data.found = NULL;
 
389
 
 
390
  gtk_tree_model_foreach (GTK_TREE_MODEL (store), find_group, &data);
 
391
 
 
392
  return data.found;
 
393
}
 
394
 
 
395
 
 
396
 
 
397
#if WITH_TESTS
 
398
 
 
399
#include <string.h>
 
400
 
 
401
int main (int argc, char **argv)
 
402
{
 
403
  KotoGroupStore *store;
 
404
  GtkTreeIter iter;
 
405
 
 
406
  g_type_init ();
 
407
 
 
408
  store = (KotoGroupStore*)koto_group_store_new
 
409
    (KOTO_GROUP_STORE_TYPE_FILTER, NULL);
 
410
  g_assert (store);
 
411
 
 
412
  koto_group_store_add_new_group (store, NULL, "Foo");
 
413
  koto_group_store_add_new_group (store, NULL, "Bar");
 
414
 
 
415
  g_assert (koto_group_store_get_iter_for_group (store, "Foo", &iter));
 
416
  g_assert (koto_group_store_get_iter_for_group (store, "Bar", &iter));
 
417
  g_assert (koto_group_store_get_iter_for_group (store, "Flob", &iter) == FALSE);
 
418
  
 
419
  g_assert (koto_group_store_match_group (store, "Flob") == NULL);
 
420
  g_assert (strcmp (koto_group_store_match_group (store, "Foo"), "Foo") == 0);
 
421
  g_assert (strcmp (koto_group_store_match_group (store, "foo"), "Foo") == 0);
 
422
  g_assert (strcmp (koto_group_store_match_group (store, "baR"), "Bar") == 0);
 
423
  
 
424
  return 0;
 
425
}
 
426
#endif