1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* ETextModelURI - A Text Model w/ clickable URIs
* Copyright (C) 2001 Ximian, Inc.
*
* Author: Jon Trowbridge <trow@gnu.org>
*
*/
#include <config.h>
#include <ctype.h>
#include "e-text-model-uri.h"
static void e_text_model_uri_class_init (ETextModelURIClass *class);
static void e_text_model_uri_init (ETextModelURI *model);
static void e_text_model_uri_destroy (GtkObject *object);
static void objectify_uris (ETextModelURI *model);
static void e_text_model_uri_set_text (ETextModel *model, gchar *text);
static const gchar *e_text_model_uri_get_nth_object (ETextModel *model, gint);
static void e_text_model_uri_activate_nth_object (ETextModel *model, gint);
static GtkObject *parent_class;
GtkType
e_text_model_uri_get_type (void)
{
static GtkType model_uri_type = 0;
if (!model_uri_type) {
GtkTypeInfo model_uri_info = {
"ETextModelURI",
sizeof (ETextModelURI),
sizeof (ETextModelURIClass),
(GtkClassInitFunc) e_text_model_uri_class_init,
(GtkObjectInitFunc) e_text_model_uri_init,
NULL, /* reserved_1 */
NULL, /* reserved_2 */
(GtkClassInitFunc) NULL
};
model_uri_type = gtk_type_unique (e_text_model_get_type (), &model_uri_info);
}
return model_uri_type;
}
static void
e_text_model_uri_class_init (ETextModelURIClass *klass)
{
GtkObjectClass *object_class;
ETextModelClass *model_class;
object_class = (GtkObjectClass *) klass;
model_class = E_TEXT_MODEL_CLASS (klass);
parent_class = gtk_type_class (e_text_model_get_type ());
object_class->destroy = e_text_model_uri_destroy;
model_class->set_text = e_text_model_uri_set_text;
model_class->get_nth_obj = e_text_model_uri_get_nth_object;
model_class->activate_nth_obj = e_text_model_uri_activate_nth_object;
}
static void
e_text_model_uri_init (ETextModelURI *model)
{
}
static void
e_text_model_uri_destroy (GtkObject *object)
{
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
static gchar *
extract_uri (gchar **in_str)
{
gchar *s = *in_str;
if (strncmp (s, "http://", 7) == 0) {
gint periods=0;
gchar *uri;
s += 7;
while (*s && (isalnum((gint) *s) || (*s == '.' && periods < 2))) {
if (*s == '.')
++periods;
++s;
}
uri = g_strndup (*in_str, s - *in_str);
*in_str = s;
return uri;
} else {
*in_str = s+1;
return NULL;
}
}
static void
objectify_uris (ETextModelURI *model_uri)
{
ETextModel *model = E_TEXT_MODEL (model_uri);
gchar *new_text;
gchar *src, *dest;
GList *uris = NULL;
if (model->text == NULL)
return;
new_text = g_new0 (gchar, strlen (model->text)+1);
src = model->text;
dest = new_text;
while (*src) {
gchar *uri_str;
gchar *next = src;
if ( (uri_str = extract_uri (&next)) ) {
uris = g_list_append (uris, uri_str);
*dest = '\1';
} else {
*dest = *src;
}
++dest;
src = next;
}
g_free (model->text);
model->text = new_text;
/* Leaking old list */
model_uri->uris = uris;
}
static void
e_text_model_uri_set_text (ETextModel *model, gchar *text)
{
if (E_TEXT_MODEL_CLASS(parent_class)->set_text)
E_TEXT_MODEL_CLASS(parent_class)->set_text (model, text);
objectify_uris (E_TEXT_MODEL_URI (model));
}
static const gchar *
e_text_model_uri_get_nth_object (ETextModel *model, gint i)
{
return (const gchar *) g_list_nth_data (E_TEXT_MODEL_URI (model)->uris, i);
}
static void
e_text_model_uri_activate_nth_object (ETextModel *model, gint i)
{
const gchar *obj_str;
obj_str = e_text_model_get_nth_object (model, i);
gnome_url_show (obj_str);
}
ETextModel *
e_text_model_uri_new (void)
{
return E_TEXT_MODEL (gtk_type_new (e_text_model_uri_get_type ()));
}
/* $Id$ */
|