~ubuntu-branches/debian/squeeze/tasks/squeeze

« back to all changes in this revision

Viewing changes to libkoto/koto-utils.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:
 
1
/*
 
2
 * Copyright (C) 2007 OpenedHand Ltd
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it under
 
5
 * the terms of the GNU General Public License as published by the Free Software
 
6
 * Foundation; either version 2 of the License, or (at your option) any later
 
7
 * version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
12
 * details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along with
 
15
 * this program; if not, write to the Free Software Foundation, Inc., 51
 
16
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 */
 
18
 
 
19
#include <config.h>
 
20
#include <glib/gi18n.h>
 
21
#include <gtk/gtk.h>
 
22
 
 
23
#include "koto-task-store.h"
 
24
#include "koto-utils.h"
 
25
 
 
26
typedef struct {
 
27
  GtkWindow *window;
 
28
  char *title;
 
29
} WindowData;
 
30
 
 
31
/*
 
32
 * Foreach function called from update_title() that simply counts the number of
 
33
 * uncompleted tasks.  @userdata is an int* to the count.
 
34
 */
 
35
static gboolean
 
36
count_pending (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 
37
{
 
38
  int *count = data;
 
39
  gboolean done;
 
40
  
 
41
  gtk_tree_model_get (model, iter, COLUMN_DONE, &done, -1);
 
42
 
 
43
  if (!done)
 
44
    (*count)++;
 
45
 
 
46
  /*
 
47
   * TODO: one day KotoTaskStore will always be sorted, but at the moment new
 
48
   * tasks are appended and then moved to the correct location. When this is
 
49
   * fixed we can return @done here and not bother counting completed tasks.
 
50
   */
 
51
  return FALSE;
 
52
}
 
53
 
 
54
/*
 
55
 * Update the window title, generally as the number of tasks has changed.
 
56
 */
 
57
static void
 
58
update_title (WindowData *data, GtkTreeModel *model)
 
59
{
 
60
  int count = 0;
 
61
  char *title;
 
62
 
 
63
  g_assert (data);
 
64
  g_assert (model);
 
65
 
 
66
  gtk_tree_model_foreach (model, count_pending, &count);
 
67
 
 
68
  title = g_strdup_printf (data->title, count);
 
69
  gtk_window_set_title (data->window, title);
 
70
  g_free (title);
 
71
}
 
72
 
 
73
/*
 
74
 * Callback when rows are inserted into the task store, to update the window
 
75
 * title.
 
76
 */
 
77
static void
 
78
on_row_inserted (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, WindowData *data)
 
79
{
 
80
  update_title (data, model);
 
81
}
 
82
 
 
83
/*
 
84
 * Callback when rows are removed from the task store, to update the window
 
85
 * title.
 
86
 */
 
87
static void
 
88
on_row_deleted (GtkTreeModel *model, GtkTreePath *path, WindowData *data)
 
89
{
 
90
  update_title (data, model);
 
91
}
 
92
 
 
93
/*
 
94
 * Weak notify for WindowData, to free it when the window is destroyed.
 
95
 */
 
96
static void
 
97
on_weak_notify (gpointer user_data, GObject *dead)
 
98
{
 
99
  WindowData *data = user_data;
 
100
  g_free (data->title);
 
101
  g_slice_free (WindowData, data);
 
102
}
 
103
 
 
104
/**
 
105
 * koto_sync_window_title:
 
106
 * @window: a #GtkWindow title to set
 
107
 * @model: a #GtkTreeModel to count tasks from
 
108
 * @title: an untranslated format string to use as the title
 
109
 *
 
110
 * Synchronise the title of @window with then number of incomplete tasks in
 
111
 * @model.  @model must be a #KotoTaskStore, or a filter model based on
 
112
 * #KotoTaskStore.  @title should be untranslated and contain a single %d
 
113
 * format, which is the number of incomplete tasks.
 
114
 */
 
115
void
 
116
koto_sync_window_title (GtkWindow *window, GtkTreeModel *model, const char *title)
 
117
{
 
118
  WindowData *data;
 
119
  
 
120
  g_return_if_fail (GTK_WINDOW (window));
 
121
  g_return_if_fail (GTK_TREE_MODEL (model));
 
122
  g_return_if_fail (title);
 
123
  
 
124
  data = g_slice_new (WindowData);
 
125
  data->window = window;
 
126
  data->title = g_strdup (title);
 
127
  
 
128
  g_object_weak_ref (G_OBJECT (model), on_weak_notify, data);
 
129
 
 
130
  g_object_connect (model,
 
131
                    "signal::row-inserted", G_CALLBACK (on_row_inserted), data,
 
132
                    "signal::row-changed", G_CALLBACK (on_row_inserted), data,
 
133
                    "signal::row-deleted", G_CALLBACK (on_row_deleted), data,
 
134
                    NULL);
 
135
 
 
136
  update_title (data, model);
 
137
}