source: titan/titan/ipkg.h @ 15271

Last change on this file since 15271 was 15261, checked in by nit, 12 years ago

[titan] extend menulist

File size: 12.1 KB
Line 
1#ifndef OIPKG_H
2#define OIPKG_H
3
4struct ipkg
5{
6        char* name;
7        char* desc;
8        char* version;
9        char* section;
10        char* showname;
11        int done;
12        struct ipkg* prev;
13        struct ipkg* next;
14};
15
16struct ipkg *ipkg = NULL;
17
18void debugipkg()
19{
20        struct ipkg *node = ipkg;
21       
22        while(node != NULL)
23        {
24                if(node->name != NULL)
25                        printf("pkg name: %s\n", node->name);
26                if(node->desc != NULL)
27                        printf("pkg desc: %s\n", node->desc);
28                if(node->version != NULL)
29                        printf("pkg version: %s\n", node->version);
30                if(node->section != NULL)
31                        printf("pkg section: %s\n", node->section);
32                if(node->showname != NULL)
33                        printf("pkg showname: %s\n", node->showname);
34                node = node->next;
35        }
36}
37
38struct ipkg* addipkg(char *name, char* desc, char* version, char* section, char* showname, struct ipkg* last)
39{
40        //debug(1000, "in");
41        struct ipkg *newnode = NULL, *prev = NULL, *node = ipkg;
42
43        newnode = (struct ipkg*)malloc(sizeof(struct ipkg));   
44        if(newnode == NULL)
45        {
46                err("no memory");
47                return NULL;
48        }
49        memset(newnode, 0, sizeof(struct ipkg));
50
51        newnode->name = ostrcat(name, NULL, 0, 0);
52        newnode->desc = ostrcat(desc, NULL, 0, 0);
53        newnode->version = ostrcat(version, NULL, 0, 0);
54        newnode->section = ostrcat(section, NULL, 0, 0);
55        newnode->showname = ostrcat(showname, NULL, 0, 0);
56
57        if(last == NULL)
58        {
59                while(node != NULL)
60                {
61                        prev = node;
62                        node = node->next;
63                }
64        }
65        else
66        {
67                prev = last;
68                node = last->next;
69        }
70
71        if(prev == NULL)
72                ipkg = newnode;
73        else
74        {
75                prev->next = newnode;
76                newnode->prev = prev;
77        }
78        newnode->next = node;
79        if(node != NULL) node->prev = newnode;
80
81        //debug(1000, "out");
82        return newnode;
83}
84
85void delipkg(struct ipkg* ipkgnode)
86{
87        //debug(1000, "in");
88        struct ipkg *node = ipkg, *prev = ipkg;
89
90        while(node != NULL)
91        {
92                if(node == ipkgnode)
93                {
94                        if(node == ipkg)
95                        {
96                                ipkg = node->next;
97                                if(ipkg != NULL)
98                                        ipkg->prev = NULL;
99                        }
100                        else
101                        {
102                                prev->next = node->next;
103                                if(node->next != NULL)
104                                        node->next->prev = prev;
105                        }
106
107                        free(node->name);
108                        node->name = NULL;
109
110                        free(node->desc);
111                        node->desc = NULL;
112
113                        free(node->version);
114                        node->version = NULL;
115
116                        free(node->section);
117                        node->section = NULL;
118
119                        free(node->showname);
120                        node->showname = NULL;
121
122                        free(node);
123                        node = NULL;
124                        break;
125                }
126
127                prev = node;
128                node = node->next;
129        }
130        //debug(1000, "out");
131}
132
133void freeipkg()
134{
135        debug(1000, "in");
136        struct ipkg *node = ipkg, *prev = ipkg;
137
138        while(node != NULL)
139        {
140                prev = node;
141                node = node->next;
142                if(prev != NULL)
143                        delipkg(prev);
144        }
145        debug(1000, "out");
146}
147
148int ipkg_list_cb(char *name, char *desc, char *version, pkg_state_status_t status, void *userdata)
149{
150        int count = 0;
151#ifndef SIMULATE
152        char* tmpstr = NULL;
153        struct splitstr* ret = NULL;
154       
155        tmpstr = ostrcat(name, NULL, 0, 0);
156        ret = strsplit(tmpstr, "-", &count);
157
158        if(count >= 4)
159        {
160                if(desc)
161                        addipkg(name, desc, version, (&ret[2])->part, (&ret[3])->part, NULL);
162                else
163                        addipkg(name, NULL, version, (&ret[2])->part, (&ret[3])->part, NULL);
164        }
165
166        free(ret); ret = NULL;
167        free(tmpstr); tmpstr = NULL;
168#endif
169
170        return 0;
171}
172
173int ipkg_status_cb(char *name, int istatus, char *desc, void *userdata)
174{
175        addipkg(name, desc, NULL, NULL, NULL, NULL);
176        return 0;
177}
178
179int ipkg_update(void)
180{
181        unlink("/var/usr/lib/ipkg/cross");
182        unlink("/var/usr/lib/ipkg/secret");
183        unlink("/var/usr/lib/ipkg/titan");             
184        int err = 0;
185#ifndef SIMULATE
186        args_t args;
187
188        args_init(&args);
189        err = ipkg_lists_update(&args);
190        args_deinit(&args);
191#endif
192        return err;
193}
194
195int ipkg_packages_list_installed(args_t *args, const char *packages, ipkg_list_callback cblist, void *userdata)
196{
197        int err = 0;
198#ifndef SIMULATE
199        ipkg_cmd_t *cmd;
200        ipkg_conf_t ipkg_conf;
201
202        err = ipkg_conf_init(&ipkg_conf, args);
203        if(err)
204        {
205                return err;
206        }
207
208        ipkg_cb_list = cblist;
209        /* we need to do this because of static declarations,
210         * maybe a good idea to change */
211        cmd = ipkg_cmd_find("list_installed");
212        if(packages)
213                err = ipkg_cmd_exec(cmd, &ipkg_conf, 1, &packages, userdata);
214        else
215                err = ipkg_cmd_exec(cmd, &ipkg_conf, 0, NULL, userdata);
216        ipkg_cb_list = NULL;
217        ipkg_conf_deinit(&ipkg_conf);
218#endif
219        return(err);
220}
221
222int ipkg_list_installed(void)
223{
224        int err = 0;
225#ifndef SIMULATE
226        args_t args;
227       
228        args_init(&args);
229        err = ipkg_packages_list_installed(&args, NULL, ipkg_list_cb, NULL);
230        args_deinit(&args);
231#endif
232        return err;
233}
234
235int ipkg_list(void)
236{
237        int err = 0;
238#ifndef SIMULATE
239        args_t args;
240       
241        args_init(&args);
242        err = ipkg_packages_list(&args, NULL, ipkg_list_cb, NULL);
243        args_deinit(&args);
244#endif
245        return err;
246}
247
248int ipkg_status(const char* package)
249{
250        int err = 0;
251#ifndef SIMULATE
252        args_t args;
253
254        args_init(&args);
255        err = ipkg_packages_status(&args, package, ipkg_status_cb, NULL);
256        args_deinit(&args);
257#endif
258        return err;
259}
260
261int ipkg_info(const char* package)
262{
263        int err = 0;
264#ifndef SIMULATE
265        args_t args;
266
267        args_init(&args);
268        err = ipkg_packages_info(&args, package, ipkg_status_cb, NULL);
269        args_deinit(&args);
270#endif
271        return err;
272}
273
274int ipkg_install(const char* package)
275{
276        debug(130, "package: %s", package);
277
278        int err = 0;
279#ifndef SIMULATE
280        args_t args;
281       
282        args_init(&args);
283        debug(130, "package1: %s", package);
284        err = ipkg_packages_install(&args, package);
285        debug(130, "package2: %s", package);
286        args_deinit(&args);
287        debug(130, "package3: %s", package);
288#endif 
289        return err;
290}
291
292int ipkg_remove(const char* package, int purge)
293{
294        int err = 0;
295#ifndef SIMULATE
296        args_t args;
297
298        args_init(&args);
299        err = ipkg_packages_remove(&args, package, purge);
300        args_deinit(&args);
301#endif
302        return err;
303}
304
305int ipkg_upgrade(void)
306{
307        int err = 0;
308#ifndef SIMULATE
309        args_t args;
310
311        args_init(&args);
312        err = ipkg_packages_upgrade(&args);
313        args_deinit(&args);
314#endif
315        return err;
316}
317
318int ipkg_download(ipkg_conf_t *conf, const char *src, const char *filename)
319{
320        int err = 0, count = 0, i = 0, withoutgui = 0;
321        char* ip = NULL, *pos = NULL, *path = NULL, *tmpstr = NULL;
322        struct splitstr* ret = NULL;
323
324        if(src == NULL) return 1;
325
326        debug(130, "src: %s", src);
327        ip = string_replace("http://", "", (char*)src, 0);
328
329        if(ip != NULL)
330                pos = strchr(ip, '/');
331        if(pos != NULL)
332        {
333                pos[0] = '\0';
334                path = pos + 1;
335        }
336
337        tmpstr = ostrcat("", path, 0, 0);
338        ret = strsplit(tmpstr, "/", &count);
339       
340        for(i = 0; i < count; i++)
341        {
342                if(ostrcmp("Packages.gz", (&ret[i])->part) == 0)
343                        withoutgui = 1;
344        }
345        free(ret); ret = NULL;
346        free(tmpstr); tmpstr = NULL;
347
348        debug(130, "src: %s", src);
349        debug(130, "ip: %s", ip);
350        debug(130, "path: %s", path);
351       
352        if(withoutgui == 1)
353        {
354                if(ostrcmp("97.74.32.10", ip) == 0)
355                {
356                        if(ostrcmp(src, "//97.74.32.10/svn/ipk/sh4/titan") != 0)                 
357                        {
358                                textbox(_("Message"), _("check your Secret Feed !"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 5, 0);               
359                                free(ip); ip = NULL;
360                                //free(path); //path = NULL;
361                                return 1;
362                        }
363                }
364
365                char* checkfile = NULL;
366                checkfile = ostrcat("/tmp/Packages.", ip, 0, 0);
367
368                if(!file_exist(checkfile)) // +status.ipkg = date + 1day
369                {
370                        char* tmppath = NULL;
371                        tmppath = ostrcat(tmppath, path, 1, 0);
372                        tmppath = string_replace("Packages.gz", "Packages.preview.tar.gz", tmppath, 1);
373                        gethttp(ip, tmppath, 80, "/tmp/Packages.preview.tar.gz", HTTPAUTH, NULL, 0);
374                        free(tmppath); tmppath = NULL;
375                       
376                        system("tar -zxvf /tmp/Packages.preview.tar.gz -C /tmp");
377                        unlink("/tmp/Packages.preview.tar.gz");
378                        writesys(checkfile, ".", 1);
379                }
380                free(checkfile); checkfile = NULL;             
381                gethttp(ip, path, 80, (char*)filename, HTTPAUTH, NULL, 0);
382        }
383        else
384                err = screendownload("Download", ip, path, 80, (char*)filename, HTTPAUTH, 0);
385        free(ip); ip = NULL;
386        //free(path);// path = NULL; segfault
387        return err;
388}
389
390int ipkg_files(const char* package)
391{
392        int err = 0;
393#ifndef SIMULATE
394        args_t args;
395
396        args_init(&args);
397        err = ipkg_package_files(&args, package, ipkg_list_cb, NULL);
398        args_deinit(&args);
399#endif
400        return err;
401}
402
403int ipkg_search(const char* package)
404{
405        int err = 0;
406#ifndef SIMULATE
407        args_t args;
408
409        args_init(&args);
410        err = ipkg_file_search(&args, package, ipkg_list_cb, NULL);
411        args_deinit(&args);
412#endif
413        return err;
414}
415
416char* get_ipk_section()
417{
418        struct ipkg *node = ipkg;
419        char* sectionlist = NULL;
420        char* namelist = NULL;
421        char* tmpstr = NULL;
422        while(node != NULL)
423        {
424                if(node->name != NULL && node->section != NULL)
425                {
426                        tmpstr = ostrcat("-", node->section, 0, 0);
427                        tmpstr = ostrcat(tmpstr, "-", 1, 0);
428                        if(!string_find(tmpstr,namelist))
429                        {
430                                namelist = ostrcat(namelist, node->name, 1, 0);
431                                namelist = ostrcat(namelist, " ", 1, 0);
432                                sectionlist = ostrcat(sectionlist, node->section, 1, 0);
433                                sectionlist = ostrcat(sectionlist, "\n", 1, 0);
434                        }
435                        free(tmpstr), tmpstr = NULL;
436                }
437                node = node->next;
438        }
439        free(namelist), namelist = NULL;
440        return sectionlist;
441}
442
443int findsectiondone(char* section)
444{
445        struct ipkg* node = ipkg;
446       
447        if(node == NULL || section == NULL) return 1;
448       
449        while(node != NULL)
450        {
451                if(node->done == 1 && ostrcmp(section, node->section) == 0)
452                        return 1;
453                node = node->next;
454        }
455
456        return 0;
457}
458
459//flag 0: show section
460//flag 1: show entrys
461//flag 2: show entrys for remove
462struct menulist* ipkmenulist(struct menulist* mlist, char* paramskinname, char* skintitle, char* paramskinpath, char* section, int showpng, int flag)
463{
464        int skip = 0;
465        struct ipkg* node = ipkg, *ipkg_installed = NULL, *node_installed = NULL;
466        struct menulist* tmpmlist = NULL;
467        char* tmpstr = NULL, *tmpinfo = NULL, *tmppic = NULL;
468       
469        if(node == NULL) return NULL;
470       
471        if(flag == 1)
472        {
473                ipkg = NULL;
474                ipkg_list_installed();
475                ipkg_installed = ipkg;
476                ipkg = node;
477        }
478       
479        while(node != NULL)
480        {
481                if(flag == 0 || flag == 2)
482                {
483                        if(flag == 0)
484                        {
485                                //check if section have seen
486                                if(findsectiondone(node->section) == 1)
487                                {
488                                        node = node->next;
489                                        continue;
490                                }
491                        }
492               
493                        tmppic = ostrcat("panel_", node->section, 0, 0);
494                        tmppic = ostrcat(tmppic, ".png", 1, 0);
495               
496                        if(flag == 0)
497                        {
498                                node->done = 1;
499                                addmenulist(&mlist, node->section, NULL, tmppic, 0, 0);
500                        }
501                       
502                        if(flag == 2)
503                        {
504                                tmpstr = ostrcat(tmpstr, node->section, 1, 0);
505                                tmpstr = ostrcat(tmpstr, "-", 1, 0);
506                                tmpstr = ostrcat(tmpstr, node->showname, 1, 0);
507                                addmenulist(&mlist, tmpstr, NULL, tmppic, 0, 0);
508                                free(tmpstr); tmpstr = NULL;
509                        }
510                       
511                        free(tmppic); tmppic = NULL;
512                }
513               
514                if(flag == 1)
515                {
516                        //check if ipkg is installed
517                        node_installed = ipkg_installed;
518                        skip = 0;
519                        while(node_installed != NULL)
520                        {
521                                if(ostrcmp(node->section, node_installed->section) == 0 && ostrcmp(node->showname, node_installed->showname) == 0)
522                                {
523                                        skip = 1;
524                                        break;
525                                }
526                                node_installed = node_installed->next;
527                        }
528                       
529                        //check if ipkg is in section
530                        if(section != NULL && ostrcmp(node->section, section) != 0)
531                                skip = 1;
532
533                        if(skip == 1)
534                        {
535                                node = node->next;
536                                continue;
537                        }
538                       
539                        tmpstr = ostrcat(tmpstr, node->showname, 1, 0);
540                        tmpstr = ostrcat(tmpstr, " v.", 1, 0);
541                        tmpstr = ostrcat(tmpstr, node->version, 1, 0);
542
543                        tmpinfo = ostrcat(tmpinfo, "\nSection: ", 1, 0);
544                        tmpinfo = ostrcat(tmpinfo, node->section, 1, 0);
545                        tmpinfo = ostrcat(tmpinfo, "\nDescription:\n", 1, 0);
546                        if(node->desc != NULL)
547                                tmpinfo = ostrcat(tmpinfo, node->desc, 1, 0);
548                        else
549                                tmpinfo = ostrcat(tmpinfo, _("no description found"), 1, 0);
550                       
551                        tmppic = ostrcat("titan-pluginpreview-", node->showname, 0, 0);
552                        tmppic = ostrcat(tmppic, ".png", 1, 0);
553
554                        tmpmlist = addmenulist(&mlist, tmpstr, tmpinfo, tmppic, 0, 0);
555                        changemenulistparam(tmpmlist, node->showname, NULL);
556                        free(tmpstr); tmpstr = NULL;
557                        free(tmpinfo); tmpinfo = NULL;
558                        free(tmppic); tmppic = NULL;
559                }
560               
561                node = node->next;
562        }
563       
564        if(flag == 1)
565        {
566                node = ipkg;
567                ipkg = ipkg_installed;
568                freeipkg();
569                ipkg = node;
570        }
571
572        return menulistbox(mlist, paramskinname, skintitle, paramskinpath, "/skin/plugin.png", showpng, 0);
573}
574
575char* get_ipk_tmpinstall(char* ipk)
576{
577        debug(130, "in");
578
579        unlink("/var/usr/lib/ipkg/cross");
580        unlink("/var/usr/lib/ipkg/secret");
581        unlink("/var/usr/lib/ipkg/titan");     
582
583        char* cmd = NULL, *tmpstr = NULL;
584        cmd = ostrcat(cmd, "ipkg install /tmp/", 1, 0);
585        cmd = ostrcat(cmd, ipk, 1, 0);
586
587        tmpstr = command(cmd);
588
589        debug(130, "out %s",cmd);
590        free(cmd); cmd = NULL;
591        return tmpstr;
592}
593
594char* get_ipk_tmplistinstall()
595{
596        debug(130, "in");
597        char* cmd = NULL, *tmpstr = NULL;
598
599        cmd = ostrcat(cmd, "ls /tmp | grep '.ipk'", 1, 0);
600
601        tmpstr = command(cmd);
602
603        debug(130, "out %s",cmd);
604        free(cmd); cmd = NULL;
605        return tmpstr;
606}
607
608#endif
609
Note: See TracBrowser for help on using the repository browser.