~ubuntu-branches/debian/wheezy/tickr/wheezy

« back to all changes in this revision

Viewing changes to src/tickr/news_list.c

  • Committer: Bazaar Package Importer
  • Author(s): Emmanuel Thomas-Maurin
  • Date: 2011-08-19 16:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110819160000-zjko4f60pdxt9i74
Tags: upstream-0.5.3
ImportĀ upstreamĀ versionĀ 0.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      NEWS - GTK-based RSS Ticker - Copyright (C) Emmanuel Thomas-Maurin 2009-2011
 
3
 *      <manutm007@gmail.com>
 
4
 *
 
5
 *      This program is free software: you can redistribute it and/or modify
 
6
 *      it under the terms of the GNU General Public License as published by
 
7
 *      the Free Software Foundation, either version 3 of the License, or
 
8
 *      (at your option) any later version.
 
9
 *
 
10
 *      This program is distributed in the hope that it will be useful,
 
11
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *      GNU General Public License for more details.
 
14
 *
 
15
 *      You should have received a copy of the GNU General Public License
 
16
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include "news.h"
 
20
 
 
21
#if USE_GUI
 
22
 
 
23
static char     url_array[NURLMAX][FILE_NAME_MAXLEN + 1];
 
24
static char     *url[NURLMAX + 1];
 
25
static char     selected_url_array[NURLMAX][FILE_NAME_MAXLEN + 1];
 
26
static char     *selected_url[NURLMAX + 1];
 
27
 
 
28
/* (re)assign all pointers to all array elements as pointer list is NULL
 
29
 * terminated - don't modify array content */
 
30
char **init_url_list()
 
31
{
 
32
        int     i = 0;
 
33
 
 
34
        for (i = 0; i < NURLMAX; i++)
 
35
                url[i] = url_array[i];
 
36
        url[NURLMAX] = NULL;
 
37
        return (char **)url;
 
38
}
 
39
 
 
40
/* (re)assign all pointers to all array elements as pointer list is NULL
 
41
 * terminated - don't modify array content */
 
42
char **init_selected_url_list()
 
43
{
 
44
        int     i = 0;
 
45
 
 
46
        for (i = 0; i < NURLMAX; i++)
 
47
                selected_url[i] = selected_url_array[i];
 
48
        selected_url[NURLMAX] = NULL;
 
49
        return (char **)selected_url;
 
50
}
 
51
 
 
52
char **get_url_list()
 
53
{
 
54
        return (char **)url;
 
55
}
 
56
 
 
57
char **get_selected_url_list()
 
58
{
 
59
        return (char **)selected_url;
 
60
}
 
61
 
 
62
/*
 
63
 * fill selected_url_array[NURLMAX][FILE_NAME_MAXLEN + 1] with lines from
 
64
 * url list file
 
65
 
 
66
 * entry format in url list file:
 
67
 *      ["*" (selected) or "-" (unselected) + url [+ ">" + title] + '\n']
 
68
 *
 
69
 * entry max length = FILE_NAME_MAXLEN
 
70
 * see also: (UN)SELECTED_URL_CHAR in news.h
 
71
 *
 
72
 * we pass a reference to (char **)selected_p_url[NURLMAX + 1] which has been
 
73
 * set as p_selected_url[i] = selected_url_array[i] and p_selected_url[NURLMAX] = NULL
 
74
 */
 
75
int load_selected_url_list(char **p_selected_url)
 
76
{
 
77
        char    tmp[FILE_NAME_MAXLEN + 1];
 
78
        int     i, j;
 
79
 
 
80
        if (load_url_list(p_selected_url) == OK) {
 
81
                for (i = 0; i < NURLMAX && p_selected_url[i] != NULL; i++) {
 
82
                        if (p_selected_url[i][0] == SELECTED_URL_CHAR) {
 
83
                                str_n_cpy(tmp, str_without_title(p_selected_url[i]), FILE_NAME_MAXLEN);
 
84
                                for (j = 0; j < FILE_NAME_MAXLEN; j++) {
 
85
                                        if (tmp[j] == '\n') {
 
86
                                                tmp[0] = '\0';
 
87
                                                break;
 
88
                                        }
 
89
                                }
 
90
                                str_n_cpy(p_selected_url[i], tmp + 1, FILE_NAME_MAXLEN);
 
91
                        } else
 
92
                                p_selected_url[i][0] = '\0';
 
93
                }
 
94
                p_selected_url[i] = NULL;
 
95
                sort_url_list(p_selected_url, 0);       /* tmp + 1 -> we compare after 1 char (* or -) */
 
96
                if (p_selected_url[0] != NULL)
 
97
                        return OK;
 
98
                else
 
99
                        return SELECTION_EMPTY;
 
100
        } else {
 
101
                p_selected_url[0] = NULL;
 
102
                return SELECTION_ERROR;
 
103
        }
 
104
}
 
105
 
 
106
/*
 
107
 * fill url_array[NURLMAX][FILE_NAME_MAXLEN + 1] with lines from url list file
 
108
 *
 
109
 * entry format in url list file:
 
110
 *      ["*" (selected) or "-" (unselected) + url [+ ">" + title] + '\n']
 
111
 *
 
112
 * entry max length = FILE_NAME_MAXLEN
 
113
 * see also: (UN)SELECTED_URL_CHAR in news.h
 
114
 *
 
115
 * we pass a reference to (char **)p_url[NURLMAX + 1] which has been set as
 
116
 * p_url[i] = url_array[i] and p_url[NURLMAX] = NULL
 
117
 */
 
118
int load_url_list(char **p_url)
 
119
{
 
120
        char    *listfname, listfname1[TMPSTR_SIZE + 1], listfname2[TMPSTR_SIZE + 1];
 
121
        FILE    *fp;
 
122
        char    *tmp1, tmp2[TMPSTR_SIZE + 1];
 
123
        size_t  tmp1_size = FILE_NAME_MAXLEN + 1;
 
124
        int     i, j;
 
125
 
 
126
        str_n_cpy(listfname1, get_datafile_full_name_from_name(URLLIST), TMPSTR_SIZE);
 
127
#ifndef G_OS_WIN32
 
128
        snprintf(listfname2, TMPSTR_SIZE + 1, "%s%c%s", INSTALL_PATH, SEPARATOR_CHAR, URLLIST);
 
129
#else
 
130
        snprintf(listfname2, TMPSTR_SIZE + 1, "%s%c%s%c%s",
 
131
                get_progfiles_dir(), SEPARATOR_CHAR, NEWS_DIR_NAME, SEPARATOR_CHAR, URLLIST);
 
132
#endif
 
133
        listfname = listfname1;
 
134
 
 
135
        if ((fp = g_fopen(listfname, "rb")) == NULL) {
 
136
                if (question_win("No URL list has been saved yet. Use sample one ?") == YES)
 
137
                        listfname = listfname2;
 
138
                else
 
139
                        return LOAD_URL_LIST_ERROR;
 
140
        }
 
141
        if ((fp = g_fopen(listfname, "rb")) != NULL) {
 
142
                tmp1 = malloc2(tmp1_size * sizeof(char));
 
143
                i = 0;
 
144
#ifndef G_OS_WIN32
 
145
                while (getline(&tmp1, &tmp1_size, fp) != EOF && i < NURLMAX) {
 
146
#else
 
147
                while (fgets(tmp1, tmp1_size, fp) != NULL && i < NURLMAX) {
 
148
#endif
 
149
                        for (j = 0; j < FILE_NAME_MAXLEN; j++)
 
150
                                if (tmp1[j] == '\n') {
 
151
                                        tmp1[j] = '\0';
 
152
                                        break;
 
153
                                }
 
154
                        if (tmp1[0] != '\0')
 
155
                                str_n_cpy(p_url[i++], tmp1, FILE_NAME_MAXLEN);
 
156
                }
 
157
                p_url[i] = NULL;
 
158
                if (i >= NURLMAX) {
 
159
                        snprintf(tmp2, TMPSTR_SIZE + 1, "Max number of URL's (= %d) is reached.\n"
 
160
                                "(You may set NURLMAX to a higher value in news.h and recompile.)",
 
161
                                NURLMAX);
 
162
                                warning(tmp2, "", "", "", FALSE);
 
163
                }
 
164
                free2(tmp1);
 
165
                fclose(fp);
 
166
                return OK;
 
167
        } else {
 
168
                warning("Can't load URL list:", listfname, "-", strerror(errno),
 
169
                        FALSE);
 
170
                return LOAD_URL_LIST_ERROR;
 
171
        }
 
172
}
 
173
 
 
174
/*
 
175
 * sort list then remove empty and duplicated entries
 
176
 * we compare after <shift> chars
 
177
 */
 
178
void sort_url_list(char **p_url, int shift)
 
179
{
 
180
        char    *p_url2[NURLMAX + 1], *tmp, empty_str[] = "";
 
181
        int     list_len, min, i, j;
 
182
 
 
183
        /*
 
184
         * copy p_url[] to p_url2[]
 
185
         */
 
186
        for (i = 0; i < NURLMAX && p_url[i] != NULL; i++)
 
187
                p_url2[i] = p_url[i];
 
188
        list_len = i;
 
189
        /*
 
190
         * sort array (selection sort)
 
191
         */
 
192
        for (i = 0; i < list_len; i++) {
 
193
                min = i;
 
194
                for (j = i  + 1; j < list_len; j++) {
 
195
                        /* + shift because we compare after shift chars */
 
196
                        if (strcmp(p_url2[min] + shift, p_url2[j] + shift) > 0)
 
197
                                min = j;
 
198
                }
 
199
                tmp = p_url2[i];
 
200
                p_url2[i] = p_url2[min];
 
201
                p_url2[min] = tmp;
 
202
        }
 
203
        /*
 
204
         * remove duplicated and empty entries
 
205
         * as we can have one entry with title (url + title) and the same without title
 
206
         * (url only), we use str_without_title() to compare strings because we want to
 
207
         * keep entries with title and remove similar entries without title
 
208
         */
 
209
        for (i = 0; i < list_len; i++)
 
210
                for (j = i  + 1; j < list_len; j++) {
 
211
                        /* + shift because we compare after shift chars */
 
212
                        if (strcmp(str_without_title(p_url2[i] + shift), str_without_title(p_url2[j] + shift)) == 0) {
 
213
                                if (!str_has_title(p_url2[i]))
 
214
                                        p_url2[i] = empty_str;
 
215
                                else
 
216
                                        p_url2[j] = empty_str;
 
217
                        }
 
218
                }
 
219
 
 
220
        for (i = 0, j = 0; i < list_len; i++)
 
221
                if (p_url2[i] != empty_str)
 
222
                        p_url[j++] = p_url2[i];
 
223
        p_url[j] = NULL;
 
224
}
 
225
 
 
226
int save_url_list(char **p_url)
 
227
{
 
228
        char    *listfname, tmp[FILE_NAME_MAXLEN + 1];
 
229
        FILE    *fp;
 
230
        int     i, j;
 
231
 
 
232
        listfname = (char *)get_datafile_full_name_from_name(URLLIST);
 
233
        if ((fp = g_fopen(listfname, "wb")) != NULL) {
 
234
                for (i = 0, j = 0; p_url[i] != NULL && j < NURLMAX; i++) {
 
235
                        str_n_cpy(tmp, p_url[i], FILE_NAME_MAXLEN);
 
236
                        if (tmp[0] != '\0') {
 
237
                                fprintf(fp, "%s\n", tmp);
 
238
                                j++;
 
239
                        }
 
240
                }
 
241
                fclose(fp);
 
242
                return OK;
 
243
        } else {
 
244
                warning("Can't save URL list:", listfname, "-", strerror(errno),
 
245
                        FALSE);
 
246
                return SAVE_URL_LIST_ERROR;
 
247
        }
 
248
}
 
249
 
 
250
/*
 
251
 * actually set entry as empty instead of removing it
 
252
 * -> need to sort list afterwards
 
253
 */
 
254
void remove_url_from_list(char **p_url, int rank)
 
255
{
 
256
        if (rank >= 0 && rank < NURLMAX)
 
257
                p_url[rank][0] = '\0';
 
258
}
 
259
 
 
260
gboolean str_has_title(char *str)
 
261
{
 
262
        int     i;
 
263
 
 
264
        for (i = 0; i < FILE_NAME_MAXLEN && str[i] != '\0'; i++)
 
265
                if (str[i]  == TITLE_TAG_CHAR)
 
266
                        return TRUE;
 
267
        return FALSE;
 
268
}
 
269
 
 
270
char *str_without_title(char *str)
 
271
{
 
272
        static char     str2[16][FILE_NAME_MAXLEN + 1];
 
273
        static int      count = -1;
 
274
        int             i;
 
275
 
 
276
        count++;
 
277
        count &= 15;
 
278
 
 
279
        str_n_cpy(str2[count], str, FILE_NAME_MAXLEN);
 
280
        for (i = 0; i < FILE_NAME_MAXLEN && str2[count][i] != '\0'; i++)
 
281
                if (str2[count][i]  == TITLE_TAG_CHAR) {
 
282
                        str2[count][i] = '\0';
 
283
                        break;
 
284
                }
 
285
        return str2[count];
 
286
}
 
287
#endif