2
* NEWS - GTK-based RSS Ticker - Copyright (C) Emmanuel Thomas-Maurin 2009-2011
3
* <manutm007@gmail.com>
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.
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.
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/>.
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];
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()
34
for (i = 0; i < NURLMAX; i++)
35
url[i] = url_array[i];
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()
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;
57
char **get_selected_url_list()
59
return (char **)selected_url;
63
* fill selected_url_array[NURLMAX][FILE_NAME_MAXLEN + 1] with lines from
66
* entry format in url list file:
67
* ["*" (selected) or "-" (unselected) + url [+ ">" + title] + '\n']
69
* entry max length = FILE_NAME_MAXLEN
70
* see also: (UN)SELECTED_URL_CHAR in news.h
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
75
int load_selected_url_list(char **p_selected_url)
77
char tmp[FILE_NAME_MAXLEN + 1];
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++) {
90
str_n_cpy(p_selected_url[i], tmp + 1, FILE_NAME_MAXLEN);
92
p_selected_url[i][0] = '\0';
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)
99
return SELECTION_EMPTY;
101
p_selected_url[0] = NULL;
102
return SELECTION_ERROR;
107
* fill url_array[NURLMAX][FILE_NAME_MAXLEN + 1] with lines from url list file
109
* entry format in url list file:
110
* ["*" (selected) or "-" (unselected) + url [+ ">" + title] + '\n']
112
* entry max length = FILE_NAME_MAXLEN
113
* see also: (UN)SELECTED_URL_CHAR in news.h
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
118
int load_url_list(char **p_url)
120
char *listfname, listfname1[TMPSTR_SIZE + 1], listfname2[TMPSTR_SIZE + 1];
122
char *tmp1, tmp2[TMPSTR_SIZE + 1];
123
size_t tmp1_size = FILE_NAME_MAXLEN + 1;
126
str_n_cpy(listfname1, get_datafile_full_name_from_name(URLLIST), TMPSTR_SIZE);
128
snprintf(listfname2, TMPSTR_SIZE + 1, "%s%c%s", INSTALL_PATH, SEPARATOR_CHAR, URLLIST);
130
snprintf(listfname2, TMPSTR_SIZE + 1, "%s%c%s%c%s",
131
get_progfiles_dir(), SEPARATOR_CHAR, NEWS_DIR_NAME, SEPARATOR_CHAR, URLLIST);
133
listfname = listfname1;
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;
139
return LOAD_URL_LIST_ERROR;
141
if ((fp = g_fopen(listfname, "rb")) != NULL) {
142
tmp1 = malloc2(tmp1_size * sizeof(char));
145
while (getline(&tmp1, &tmp1_size, fp) != EOF && i < NURLMAX) {
147
while (fgets(tmp1, tmp1_size, fp) != NULL && i < NURLMAX) {
149
for (j = 0; j < FILE_NAME_MAXLEN; j++)
150
if (tmp1[j] == '\n') {
155
str_n_cpy(p_url[i++], tmp1, FILE_NAME_MAXLEN);
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.)",
162
warning(tmp2, "", "", "", FALSE);
168
warning("Can't load URL list:", listfname, "-", strerror(errno),
170
return LOAD_URL_LIST_ERROR;
175
* sort list then remove empty and duplicated entries
176
* we compare after <shift> chars
178
void sort_url_list(char **p_url, int shift)
180
char *p_url2[NURLMAX + 1], *tmp, empty_str[] = "";
181
int list_len, min, i, j;
184
* copy p_url[] to p_url2[]
186
for (i = 0; i < NURLMAX && p_url[i] != NULL; i++)
187
p_url2[i] = p_url[i];
190
* sort array (selection sort)
192
for (i = 0; i < list_len; 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)
200
p_url2[i] = p_url2[min];
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
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;
216
p_url2[j] = empty_str;
220
for (i = 0, j = 0; i < list_len; i++)
221
if (p_url2[i] != empty_str)
222
p_url[j++] = p_url2[i];
226
int save_url_list(char **p_url)
228
char *listfname, tmp[FILE_NAME_MAXLEN + 1];
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);
244
warning("Can't save URL list:", listfname, "-", strerror(errno),
246
return SAVE_URL_LIST_ERROR;
251
* actually set entry as empty instead of removing it
252
* -> need to sort list afterwards
254
void remove_url_from_list(char **p_url, int rank)
256
if (rank >= 0 && rank < NURLMAX)
257
p_url[rank][0] = '\0';
260
gboolean str_has_title(char *str)
264
for (i = 0; i < FILE_NAME_MAXLEN && str[i] != '\0'; i++)
265
if (str[i] == TITLE_TAG_CHAR)
270
char *str_without_title(char *str)
272
static char str2[16][FILE_NAME_MAXLEN + 1];
273
static int count = -1;
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';