~ubuntu-branches/ubuntu/maverick/gwhere/maverick

« back to all changes in this revision

Viewing changes to src/data/gwcatalogfileinfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Sébastien LECACHEUR
  • Date: 2004-09-27 00:41:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040927004100-q23139ty7b94kryi
Tags: upstream-0.1.6
Import upstream version 0.1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  GWhere
 
2
 *  Copyright (C) 2000  S�bastien LECACHEUR
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
 
 
20
#include "../gwsupport.h"
 
21
#include "gwcatalogfileinfo.h"
 
22
 
 
23
#include <string.h> /* strcmp */
 
24
#include <sys/stat.h> /* stats */
 
25
 
 
26
 
 
27
typedef struct catalog_file_info
 
28
{
 
29
        gchar *title;                           /* Catalog name                                 */
 
30
        gchar *name;                            /* Catalog file name                    */
 
31
        gchar *full_name;                       /* Catalog full path name               */
 
32
        gchar *version;                         /* Catalog version                              */
 
33
        gchar *program_maker;           /* Catalog maker name                   */
 
34
        GList *categories;                      /* Categories list                              */
 
35
        gchar *description;                     /* Catalog description                  */
 
36
        gboolean ismodified;            /* If the catalog is modified   */
 
37
} CatalogFileInfo;
 
38
 
 
39
 
 
40
struct catalog_file_info * catalog_file_info_new ( void)
 
41
{
 
42
        struct catalog_file_info *p = NULL;
 
43
        struct category *category = NULL;
 
44
 
 
45
 
 
46
#ifdef GW_DEBUG_DATA_COMPONENT
 
47
        static gint i = 0;
 
48
 
 
49
 
 
50
        i++;
 
51
 
 
52
        g_print ( "*** GW - %s (%d) :: %s() : %d calls\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, i);
 
53
#endif
 
54
 
 
55
        if ( (p = (struct catalog_file_info*)g_malloc0 ( sizeof ( struct catalog_file_info))) != NULL )
 
56
        {
 
57
                p->name = NULL;
 
58
                p->title = NULL;
 
59
                p->full_name = NULL;
 
60
                p->version = NULL;
 
61
                p->program_maker = NULL;
 
62
                p->categories = NULL;
 
63
                p->description = NULL;
 
64
                p->ismodified = FALSE;
 
65
        }
 
66
 
 
67
        /* Warning it's a dirty code!! */
 
68
        category = category_new ( );
 
69
        category_set_name ( category, g_strdup ( _( "none")));
 
70
        catalog_file_info_append_category ( p, category);
 
71
 
 
72
        return p;
 
73
}
 
74
 
 
75
 
 
76
gint catalog_file_info_set_title ( struct catalog_file_info *p, gchar *title)
 
77
{
 
78
#ifdef GW_DEBUG_DATA_COMPONENT
 
79
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
80
#endif
 
81
 
 
82
        if ( p != NULL )
 
83
        {
 
84
                if ( p->title != NULL )
 
85
                {
 
86
                        g_free ( p->title);
 
87
                }
 
88
 
 
89
                p->title = title;
 
90
 
 
91
                return 0;
 
92
        }
 
93
 
 
94
        return -1;
 
95
}
 
96
 
 
97
 
 
98
gint catalog_file_info_set_name ( struct catalog_file_info *p, gchar *name)
 
99
{
 
100
#ifdef GW_DEBUG_DATA_COMPONENT
 
101
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
102
#endif
 
103
 
 
104
        if ( p != NULL )
 
105
        {
 
106
                if ( p->name != NULL )
 
107
                {
 
108
                        g_free ( p->name);
 
109
                }
 
110
 
 
111
                p->name = name;
 
112
 
 
113
                return 0;
 
114
        }
 
115
 
 
116
        return -1;
 
117
}
 
118
 
 
119
 
 
120
gint catalog_file_info_set_full_name ( struct catalog_file_info *p, gchar *full_name)
 
121
{
 
122
#ifdef GW_DEBUG_DATA_COMPONENT
 
123
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
124
#endif
 
125
 
 
126
        if ( p != NULL )
 
127
        {
 
128
                if ( p->full_name != NULL )
 
129
                {
 
130
                        g_free ( p->full_name);
 
131
                }
 
132
 
 
133
                p->full_name = full_name;
 
134
 
 
135
                return 0;
 
136
        }
 
137
 
 
138
        return -1;
 
139
}
 
140
 
 
141
 
 
142
gint catalog_file_info_set_version ( struct catalog_file_info *p, gchar *version)
 
143
{
 
144
#ifdef GW_DEBUG_DATA_COMPONENT
 
145
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
146
#endif
 
147
 
 
148
        if ( p != NULL )
 
149
        {
 
150
                if ( p->version != NULL )
 
151
                {
 
152
                        g_free ( p->version);
 
153
                }
 
154
 
 
155
                p->version = version;
 
156
 
 
157
                return 0;
 
158
        }
 
159
 
 
160
        return -1;
 
161
}
 
162
 
 
163
 
 
164
gint catalog_file_info_set_program_maker ( struct catalog_file_info *p, gchar *program_maker)
 
165
{
 
166
#ifdef GW_DEBUG_DATA_COMPONENT
 
167
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
168
#endif
 
169
 
 
170
        if ( p != NULL )
 
171
        {
 
172
                if ( p->program_maker != NULL )
 
173
                {
 
174
                        g_free ( p->program_maker);
 
175
                }
 
176
 
 
177
                p->program_maker = program_maker;
 
178
 
 
179
                return 0;
 
180
        }
 
181
 
 
182
        return -1;
 
183
}
 
184
 
 
185
 
 
186
gint catalog_file_info_set_categories ( struct catalog_file_info *p, GList *categories)
 
187
{
 
188
#ifdef GW_DEBUG_DATA_COMPONENT
 
189
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
190
#endif
 
191
 
 
192
        if ( p != NULL )
 
193
        {
 
194
                if ( p->categories != NULL )
 
195
                {
 
196
                        g_list_foreach ( p->categories, (GFunc)g_free, NULL);
 
197
                        g_free ( p->categories);
 
198
                }
 
199
 
 
200
                p->categories = g_list_first ( categories);
 
201
 
 
202
                return 0;
 
203
        }
 
204
 
 
205
        return -1;
 
206
}
 
207
 
 
208
 
 
209
gint catalog_file_info_append_category ( struct catalog_file_info *p, struct category *category)
 
210
{
 
211
#ifdef GW_DEBUG_DATA_COMPONENT
 
212
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
213
#endif
 
214
 
 
215
        if ( p != NULL )
 
216
        {
 
217
                if ( category != NULL )
 
218
                {
 
219
                        p->categories = g_list_append ( p->categories, category);
 
220
//                      category_set_index ( category, g_list_position ( g_list_first ( p->categories), p->categories));
 
221
                        category_set_index ( category, g_list_length ( g_list_first ( p->categories)) - 1);
 
222
 
 
223
                        return 0;
 
224
                }
 
225
                else
 
226
                {
 
227
                        return -1;
 
228
                }
 
229
        }
 
230
 
 
231
        return -1;
 
232
}
 
233
 
 
234
 
 
235
gint catalog_file_info_remove_category ( struct catalog_file_info *p, struct category *category)
 
236
{
 
237
        gint index, i, size;
 
238
        GList *categories = NULL;
 
239
 
 
240
 
 
241
#ifdef GW_DEBUG_DATA_COMPONENT
 
242
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
243
#endif
 
244
 
 
245
        if ( p != NULL )
 
246
        {
 
247
                if ( category != NULL )
 
248
                {
 
249
                        categories = catalog_file_info_get_categories ( p);
 
250
                        size = g_list_length ( g_list_first ( categories)) - 1;
 
251
                        index = category_get_index ( category);
 
252
 
 
253
                        categories = g_list_remove ( categories, category);
 
254
                        categories = g_list_first ( categories);
 
255
                        p->categories = g_list_first ( categories);
 
256
 
 
257
                        categories = g_list_nth ( categories, index);
 
258
 
 
259
                        for ( i = index; i < size; i++)
 
260
                        {
 
261
                                category_set_index ( categories->data, category_get_index ( categories->data) - 1);
 
262
                                categories = g_list_next ( categories);
 
263
                        }
 
264
 
 
265
                        return 0;
 
266
                }
 
267
                else
 
268
                {
 
269
                        return -1;
 
270
                }
 
271
        }
 
272
 
 
273
        return -1;
 
274
}
 
275
 
 
276
 
 
277
gint catalog_file_info_set_description ( struct catalog_file_info *p, gchar *description)
 
278
{
 
279
#ifdef GW_DEBUG_DATA_COMPONENT
 
280
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
281
#endif
 
282
 
 
283
        if ( p != NULL )
 
284
        {
 
285
                if ( p->description != NULL )
 
286
                {
 
287
                        g_free ( p->description);
 
288
                }
 
289
 
 
290
                p->description = description;
 
291
 
 
292
                return 0;
 
293
        }
 
294
 
 
295
        return -1;
 
296
}
 
297
 
 
298
 
 
299
gint catalog_file_info_set_ismodified ( struct catalog_file_info *p, gboolean b)
 
300
{
 
301
#ifdef GW_DEBUG_DATA_COMPONENT
 
302
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
303
#endif
 
304
 
 
305
        if ( p != NULL )
 
306
        {
 
307
                p->ismodified = b;
 
308
 
 
309
                return 0;
 
310
        }
 
311
 
 
312
        return -1;
 
313
}
 
314
 
 
315
 
 
316
gchar * catalog_file_info_get_title ( struct catalog_file_info *p)
 
317
{
 
318
#ifdef GW_DEBUG_DATA_COMPONENT
 
319
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
320
#endif
 
321
 
 
322
        if ( p != NULL )
 
323
        {
 
324
                if ( p->title != NULL )
 
325
                {
 
326
                        return p->title;
 
327
                }
 
328
                else
 
329
                {
 
330
                        return "";
 
331
                }
 
332
        }
 
333
 
 
334
        return NULL;
 
335
}
 
336
 
 
337
 
 
338
gchar * catalog_file_info_get_name ( struct catalog_file_info *p)
 
339
{
 
340
#ifdef GW_DEBUG_DATA_COMPONENT
 
341
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
342
#endif
 
343
 
 
344
        if ( p != NULL )
 
345
        {
 
346
                if ( p->name != NULL )
 
347
                {
 
348
                        return p->name;
 
349
                }
 
350
                else
 
351
                {
 
352
                        return "";
 
353
                }
 
354
        }
 
355
 
 
356
        return NULL;
 
357
}
 
358
 
 
359
 
 
360
gchar * catalog_file_info_get_full_name ( struct catalog_file_info *p)
 
361
{
 
362
#ifdef GW_DEBUG_DATA_COMPONENT
 
363
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
364
#endif
 
365
 
 
366
        if ( p != NULL )
 
367
        {
 
368
                if ( p->full_name != NULL )
 
369
                {
 
370
                        return p->full_name;
 
371
                }
 
372
                else
 
373
                {
 
374
                        return "";
 
375
                }
 
376
        }
 
377
 
 
378
        return NULL;
 
379
}
 
380
 
 
381
 
 
382
gchar * catalog_file_info_get_version ( struct catalog_file_info *p)
 
383
{
 
384
#ifdef GW_DEBUG_DATA_COMPONENT
 
385
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
386
#endif
 
387
 
 
388
        if ( p != NULL )
 
389
        {
 
390
                if ( p->version != NULL )
 
391
                {
 
392
                        return p->version;
 
393
                }
 
394
                else
 
395
                {
 
396
                        return "";
 
397
                }
 
398
        }
 
399
 
 
400
        return NULL;
 
401
}
 
402
 
 
403
 
 
404
gchar * catalog_file_info_get_program_maker ( struct catalog_file_info *p)
 
405
{
 
406
#ifdef GW_DEBUG_DATA_COMPONENT
 
407
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
408
#endif
 
409
 
 
410
        if ( p != NULL )
 
411
        {
 
412
                if ( p->program_maker != NULL )
 
413
                {
 
414
                        return p->program_maker;
 
415
                }
 
416
                else
 
417
                {
 
418
                        return "";
 
419
                }
 
420
        }
 
421
 
 
422
        return NULL;
 
423
}
 
424
 
 
425
 
 
426
GList * catalog_file_info_get_categories ( struct catalog_file_info *p)
 
427
{
 
428
#ifdef GW_DEBUG_DATA_COMPONENT
 
429
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
430
#endif
 
431
 
 
432
        if ( p != NULL )
 
433
        {
 
434
#ifdef GW_DEBUG_DATA_COMPONENT
 
435
        g_print ( "*** GW - %s (%d) :: %s() : categories list isn't null.\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
436
#endif
 
437
 
 
438
                return p->categories;
 
439
        }
 
440
 
 
441
#ifdef GW_DEBUG_DATA_COMPONENT
 
442
        g_print ( "*** GW - %s (%d) :: %s() : categories list is null!!\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
443
#endif
 
444
 
 
445
        return NULL;
 
446
}
 
447
 
 
448
struct category * catalog_file_info_get_category ( struct catalog_file_info *p, gchar *category)
 
449
{
 
450
        gint i = 0, size= 0;
 
451
        GList *categories = NULL;
 
452
 
 
453
 
 
454
#ifdef GW_DEBUG_DATA_COMPONENT
 
455
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
456
#endif
 
457
 
 
458
        categories = catalog_file_info_get_categories ( p);
 
459
        categories = g_list_first ( categories);
 
460
        size = g_list_length ( categories);
 
461
 
 
462
        for ( i = 0; i < size; i++)
 
463
        {
 
464
                if ( strcmp ( category, category_get_name ( categories->data)) == 0 )
 
465
                {
 
466
                        return categories->data;
 
467
                }
 
468
 
 
469
                categories = g_list_next ( categories);
 
470
        }
 
471
 
 
472
        return catalog_file_info_get_category ( p, "none");
 
473
}
 
474
 
 
475
 
 
476
struct category * catalog_file_info_get_category_by_index ( struct catalog_file_info *p, guint index)
 
477
{
 
478
        GList *categories = NULL;
 
479
 
 
480
 
 
481
#ifdef GW_DEBUG_DATA_COMPONENT
 
482
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
483
#endif
 
484
 
 
485
        categories = catalog_file_info_get_categories ( p);
 
486
 
 
487
        if ( index > g_list_length ( categories) )
 
488
        {
 
489
                return catalog_file_info_get_category_by_index ( p, 0);
 
490
        }
 
491
 
 
492
        categories = g_list_nth ( categories, index);
 
493
 
 
494
        return categories->data;
 
495
}
 
496
 
 
497
 
 
498
gchar * catalog_file_info_get_description ( struct catalog_file_info *p)
 
499
{
 
500
#ifdef GW_DEBUG_DATA_COMPONENT
 
501
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
502
#endif
 
503
 
 
504
        if ( p != NULL )
 
505
        {
 
506
                if ( p->description != NULL )
 
507
                {
 
508
                        return p->description;
 
509
                }
 
510
                else
 
511
                {
 
512
                        return "";
 
513
                }
 
514
        }
 
515
 
 
516
        return NULL;
 
517
}
 
518
 
 
519
 
 
520
gulong catalog_file_info_get_size ( struct catalog_file_info *p)
 
521
{
 
522
        struct stat f_info;
 
523
 
 
524
 
 
525
#ifdef GW_DEBUG_DATA_COMPONENT
 
526
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
527
#endif
 
528
 
 
529
        if ( p != NULL )
 
530
        {
 
531
                if ( stat( catalog_file_info_get_full_name ( p), &f_info) != -1 )
 
532
                {
 
533
                        return f_info.st_size;
 
534
                }
 
535
        }
 
536
 
 
537
        return 0;
 
538
}
 
539
 
 
540
 
 
541
gboolean catalog_file_info_get_ismodified ( struct catalog_file_info *p)
 
542
{
 
543
#ifdef GW_DEBUG_DATA_COMPONENT
 
544
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
545
#endif
 
546
 
 
547
        if ( p != NULL )
 
548
        {
 
549
                return p->ismodified;
 
550
        }
 
551
 
 
552
        /* p doesn't exists, it may not be modified.*/
 
553
        return FALSE;
 
554
}
 
555
 
 
556
 
 
557
gint catalog_file_info_category_free ( struct category *category, gpointer data)
 
558
{
 
559
        gint result = -1;
 
560
 
 
561
 
 
562
#ifdef GW_DEBUG_DATA_COMPONENT
 
563
        g_print ( "*** GW - %s (%d) :: %s()\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);
 
564
#endif
 
565
 
 
566
        if ( category != NULL )
 
567
        {
 
568
                category_free ( category);
 
569
 
 
570
                result = 0;
 
571
        }
 
572
 
 
573
        return result;
 
574
}
 
575
 
 
576
 
 
577
gboolean catalog_file_info_free ( struct catalog_file_info *p)
 
578
{
 
579
#ifdef GW_DEBUG_DATA_COMPONENT
 
580
        static gint i = 0;
 
581
 
 
582
 
 
583
        i++;
 
584
 
 
585
        g_print ( "*** GW - %s (%d) :: %s() : %d calls\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, i);
 
586
#endif
 
587
 
 
588
        if ( p != NULL )
 
589
        {
 
590
                catalog_file_info_set_name ( p, NULL);
 
591
                catalog_file_info_set_title ( p, NULL);
 
592
                catalog_file_info_set_full_name ( p, NULL);
 
593
                catalog_file_info_set_version ( p, NULL);
 
594
                catalog_file_info_set_program_maker ( p, NULL);
 
595
 
 
596
                if ( p->categories != NULL )
 
597
                {
 
598
                        g_list_foreach ( p->categories, (GFunc)catalog_file_info_category_free, NULL);
 
599
                        g_list_free ( p->categories);
 
600
                }
 
601
 
 
602
                catalog_file_info_set_description ( p, NULL);
 
603
 
 
604
                g_free ( p);
 
605
 
 
606
                return TRUE;
 
607
        }
 
608
 
 
609
        return FALSE;
 
610
}