~ubuntu-branches/ubuntu/intrepid/almanah/intrepid

« back to all changes in this revision

Viewing changes to src/link.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-07-04 15:07:43 UTC
  • Revision ID: james.westby@ubuntu.com-20080704150743-tgp3cljsjpolgv00
Tags: upstream-0.4.0
ImportĀ upstreamĀ versionĀ 0.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
 * Diary
 
4
 * Copyright (C) Philip Withnall 2008 <philip@tecnocode.co.uk>
 
5
 * 
 
6
 * Diary is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * Diary is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with Diary.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <glib.h>
 
21
#include <math.h>
 
22
#include <glib/gi18n.h>
 
23
#include <string.h>
 
24
 
 
25
#include "main.h"
 
26
#include "link.h"
 
27
 
 
28
#define LINK_TYPE(T) gchar *link_##T##_format_value (const DiaryLink *link); \
 
29
gboolean link_##T##_view (const DiaryLink *link); \
 
30
void link_##T##_build_dialog (const gchar *type, GtkTable *dialog_table); \
 
31
void link_##T##_get_values (DiaryLink *link);
 
32
 
 
33
/*LINK_TYPE (email)*/
 
34
LINK_TYPE (uri)
 
35
LINK_TYPE (file)
 
36
LINK_TYPE (note)
 
37
 
 
38
/*
 
39
 * IMPORTANT:
 
40
 * Make sure this list is kept in alphabetical order by type.
 
41
 *
 
42
 * To add a link type, add an entry here then add a file in src/links
 
43
 * named after the link type, containing the format, view, dialogue-building
 
44
 * and value-getting functions referenced in this table.
 
45
 * Don't forget to add the function prototypes at the top of *this* file.
 
46
 */
 
47
static const DiaryLinkType link_types[] = {
 
48
        /* Type,        Name,                   Description,                            Icon,                           Columns,        Format function,                View function,          Dialogue build function,        Get values function */
 
49
        /*{ "email",    N_("E-mail"),           N_("An e-mail you sent or received."),  "mail-read",                    2,              &link_email_format_value,       &link_email_view,       &link_email_build_dialog,       &link_email_get_values },*/
 
50
 
 
51
        /* Translators: These are the names and descriptions of the different link types. */
 
52
        { "file",       N_("File"),             N_("An attached file."),                "system-file-manager",          1,              &link_file_format_value,        &link_file_view,        &link_file_build_dialog,        &link_file_get_values },
 
53
        { "note",       N_("Note"),             N_("A note about an important event."), "emblem-important",             1,              &link_note_format_value,        &link_note_view,        &link_note_build_dialog,        &link_note_get_values },
 
54
        { "uri",        N_("URI"),              N_("A URI of a file or web page."),     "applications-internet",        1,              &link_uri_format_value,         &link_uri_view,         &link_uri_build_dialog,         &link_uri_get_values }
 
55
};
 
56
 
 
57
void
 
58
diary_populate_link_model (GtkListStore *list_store, guint type_column, guint name_column, guint icon_name_column)
 
59
{
 
60
        GtkTreeIter iter;
 
61
        guint i;
 
62
 
 
63
        for (i = 0; i < G_N_ELEMENTS (link_types); i++) {
 
64
                gtk_list_store_append (list_store, &iter);
 
65
                gtk_list_store_set (list_store, &iter,
 
66
                                    type_column, link_types[i].type,
 
67
                                    name_column, _(link_types[i].name),
 
68
                                    icon_name_column, link_types[i].icon_name,
 
69
                                    -1);
 
70
        }
 
71
}
 
72
 
 
73
gboolean
 
74
diary_validate_link_type (const gchar *type)
 
75
{
 
76
        return (diary_link_get_type (type) == NULL) ? FALSE : TRUE;
 
77
}
 
78
 
 
79
const DiaryLinkType *
 
80
diary_link_get_type (const gchar *type)
 
81
{
 
82
        guint lower_limit, upper_limit, temp;
 
83
        gint comparison;
 
84
 
 
85
        /* Do a binary search */
 
86
        lower_limit = 0;
 
87
        upper_limit = G_N_ELEMENTS (link_types) - 1;
 
88
 
 
89
        /* TODO: perhaps use GQuarks to make things less heavy on the strcmps */
 
90
        do {
 
91
                temp = ceil ((lower_limit + upper_limit) / 2);
 
92
                comparison = strcmp (type, link_types[temp].type);
 
93
 
 
94
                /* Exit condition */
 
95
                if (lower_limit == upper_limit && comparison != 0)
 
96
                        return NULL;
 
97
 
 
98
                if (comparison < 0)
 
99
                        upper_limit = temp - 1; /* It's in the lower half */
 
100
                else if (comparison > 0)
 
101
                        lower_limit = temp + 1; /* It's in the upper half */
 
102
                else
 
103
                        return &(link_types[temp]); /* Match! */
 
104
        } while (TRUE);
 
105
 
 
106
        return NULL;
 
107
}
 
108
 
 
109
gchar *
 
110
diary_link_format_value (const DiaryLink *link)
 
111
{
 
112
        const DiaryLinkType *link_type = diary_link_get_type (link->type);
 
113
        g_assert (link_type != NULL);
 
114
        return link_type->format_value_func (link);
 
115
}
 
116
 
 
117
gboolean
 
118
diary_link_view (const DiaryLink *link)
 
119
{
 
120
        const DiaryLinkType *link_type = diary_link_get_type (link->type);
 
121
        g_assert (link_type != NULL);
 
122
 
 
123
        if (diary->debug)
 
124
                g_debug ("Viewing %s link ('%s', '%s')", link->type, link->value, link->value2);
 
125
 
 
126
        return link_type->view_func (link);
 
127
}
 
128
 
 
129
void
 
130
diary_link_build_dialog (const DiaryLinkType *link_type)
 
131
{
 
132
        g_assert (link_type != NULL);
 
133
        link_type->build_dialog_func (link_type->type, diary->ald_table);
 
134
}
 
135
 
 
136
void
 
137
diary_link_get_values (DiaryLink *link)
 
138
{
 
139
        const DiaryLinkType *link_type = diary_link_get_type (link->type);
 
140
        g_assert (link_type != NULL);
 
141
        link_type->get_values_func (link);
 
142
}