source: titan/titan/mediadb.h @ 16923

Last change on this file since 16923 was 16923, checked in by nit, 11 years ago

fix mediadb

File size: 42.3 KB
Line 
1#ifndef MEDIADB_H
2#define MEDIADB_H
3
4void debugimdbnode(struct imdb* node)
5{
6  if(node != NULL)
7  {
8        debug(133, "----------------------mediadb start----------------------");
9        debug(133, "use id: %s", node->id);
10        debug(133, "use title: %s", node->title);
11        debug(133, "use genre: %s", node->genre);
12        debug(133, "use writer: %s", node->writer);
13        debug(133, "use director: %s", node->director);
14        debug(133, "use released: %s", node->released);
15        debug(133, "use actors: %s", node->actors);
16        debug(133, "use plot: %s", node->plot);
17        debug(133, "use poster: %s", node->poster);
18        debug(133, "use rating: %s", node->rating);
19        debug(133, "use votes: %s", node->votes);
20        debug(133, "use runtime: %s", node->runtime);
21        debug(133, "use year: %s", node->year);
22        debug(133, "use rated: %s", node->rated);
23        debug(133, "----------------------mediadb end----------------------");
24  }
25}
26
27//flag 0: with lock
28//flag 1: without lock
29struct mediadbfilter* getmediadbfilter(struct mediadb* mnode, int flag)
30{
31        if(flag == 0) m_lock(&status.mediadbmutex, 17);
32        struct mediadbfilter *node = mediadbfilter;
33
34        while(node != NULL)
35        {
36                if(node->node == mnode)
37                {
38                        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
39                        return node;
40                }
41                node = node->next;
42        }
43
44        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
45        return NULL;
46}
47
48int getmediadbfiltercount()
49{
50        int count = 0;
51
52        m_lock(&status.mediadbmutex, 17);
53        struct mediadbfilter *node = mediadbfilter;
54
55        while(node != NULL)
56        {
57                count++;
58                node = node->next;
59        }
60
61        m_unlock(&status.mediadbmutex, 17);
62        return count;
63}
64
65struct mediadbfilter* getmediadbfilterrandom(int maxentry)
66{
67        int count = 0;
68
69        if(maxentry < 1) return NULL;
70
71        m_lock(&status.mediadbmutex, 17);
72        struct mediadbfilter *node = mediadbfilter;
73
74        srand(time(NULL));
75        int r = rand() % maxentry;
76        r++;
77
78        while(node != NULL)
79        {
80                count++;
81                if(count == r) break;
82
83                node = node->next;
84        }
85
86        m_unlock(&status.mediadbmutex, 17);
87        return node;
88}
89
90//flag 0: with lock
91//flag 1: without lock
92struct mediadbfilter* getlastmediadbfilter(struct mediadbfilter* node, int flag)
93{
94        if(flag == 0) m_lock(&status.mediadbmutex, 17);
95        struct mediadbfilter *prev = NULL;
96
97        while(node != NULL)
98        {
99                prev = node;
100                node = node->next;
101        }
102
103        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
104        return prev;
105}
106
107void getmediadbcounts(int* video, int* audio, int* picture)
108{
109        m_lock(&status.mediadbmutex, 17);
110        struct mediadb* node = mediadb;
111
112        while(node != NULL)
113        {
114                if(node->type == 0)
115                        (*video)++;
116                else if(node->type == 1)
117                        (*audio)++;
118                else if(node->type == 2)
119                        (*picture)++;
120
121                node = node->next;
122        }
123
124        m_unlock(&status.mediadbmutex, 17);
125}
126
127//flag 0: with lock
128//flag 1: without lock
129struct mediadb* getlastmediadb(struct mediadb* node, int flag)
130{
131        struct mediadb *prev = NULL;
132
133        if(flag == 0) m_lock(&status.mediadbmutex, 17);
134        while(node != NULL)
135        {
136                prev = node;
137                node = node->next;
138        }
139
140        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
141        return prev;
142}
143
144int movemediadbdown(struct mediadb* node)
145{
146        struct mediadb* prev = NULL, *next = NULL;
147
148        if(node == NULL || mediadb == NULL)
149        {
150                debug(1000, "NULL detect");
151                return 1;
152        }
153
154        m_lock(&status.mediadbmutex, 17);
155
156        //last node
157        if(node->next == NULL)
158        {
159                if(node->prev != NULL)
160                        node->prev->next = NULL;
161                node->prev = NULL;
162                node->next = mediadb;
163                mediadb->prev = node;
164                mediadb = node;
165                m_unlock(&status.mediadbmutex, 17);
166                return 0;
167        }
168
169        //haenge node aus
170        if(node->prev != NULL)
171                node->prev->next = node->next;
172        else
173                mediadb = node->next;
174        node->next->prev = node->prev;
175
176        //save nodes next and prev
177        next = node->next;
178        prev = node->prev;
179
180        //haenge es eine pos nacher ein
181        node->next = next->next;
182        node->prev = next;
183       
184        if(next->next != NULL)
185                next->next->prev = node;
186        next->next = node;
187
188        m_unlock(&status.mediadbmutex, 17);
189        status.writemediadb = 1;
190        return 0;
191}
192
193int movemediadbup(struct mediadb* node)
194{
195        struct mediadb* prev = NULL, *next = NULL, *last = NULL;
196
197        if(node == NULL || mediadb == NULL)
198        {
199                debug(1000, "NULL detect");
200                return 1;
201        }
202
203        m_lock(&status.mediadbmutex, 17);
204
205        //first node
206        if(node->prev == NULL)
207        {
208                last = getlastmediadb(mediadb, 1);
209
210                if(node->next != NULL)
211                        node->next->prev = NULL;
212                mediadb = node->next;
213                node->next = NULL;
214                last->next = node;
215                node->prev = last;
216                m_unlock(&status.mediadbmutex, 17);
217                return 0;
218        }
219
220        //haenge node aus
221        node->prev->next = node->next;
222        if(node->next != NULL)
223                node->next->prev = node->prev;
224
225        //save nodes next and prev
226        next = node->next;
227        prev = node->prev;
228
229        //haenge es eine pos voher ein
230        node->next = prev;
231        node->prev = prev->prev;
232       
233        if(prev->prev != NULL)
234                prev->prev->next = node;
235        else
236                mediadb = node;
237        prev->prev = node;
238
239        m_unlock(&status.mediadbmutex, 17);
240        status.writemediadb = 1;
241        return 0;
242}
243
244struct mediadbfilter* addmediadbfilter(struct mediadb* mnode, int nr, int count, struct mediadbfilter* last)
245{
246        struct mediadbfilter *newnode = NULL, *prev = NULL, *node = NULL;
247
248        if(mnode == NULL) return NULL;
249
250        newnode = (struct mediadbfilter*)calloc(1, sizeof(struct mediadbfilter));
251        if(newnode == NULL)
252        {
253                err("no memory");
254                return NULL;
255        }
256
257        newnode->node = mnode;
258        newnode->count = nr;
259
260        m_lock(&status.mediadbmutex, 17);
261        node = mediadbfilter;
262
263        if(last == NULL)
264        {
265                //while(node != NULL && strcasecmp(newnode->node->title, node->node->title) > 0)
266                while(node != NULL)
267                {
268                        prev = node;
269                        node = node->next;
270                }
271        }
272        else
273        {
274                prev = last;
275                node = last->next;
276        }
277
278        if(prev == NULL)
279                mediadbfilter = newnode;
280        else
281        {
282                prev->next = newnode;
283                newnode->prev = prev;
284        }
285        newnode->next = node;
286        if(node != NULL) node->prev = newnode;
287       
288        m_unlock(&status.mediadbmutex, 17);
289        //debug(1000, "out");
290        return newnode;
291}
292
293//flag 0: with lock
294//flag 1: without lock
295struct mediadbcategory* addmediadbcategory(char* line, int type, int count, struct mediadbcategory* last, int flag)
296{
297        struct mediadbcategory *newnode = NULL, *prev = NULL, *node = NULL;
298        char* name = NULL;
299        int ret = 0;
300
301        if(line == NULL) return NULL;
302
303        newnode = (struct mediadbcategory*)calloc(1, sizeof(struct mediadbcategory));
304        if(newnode == NULL)
305        {
306                err("no memory");
307                return NULL;
308        }
309       
310        name = malloc(256);
311        if(name == NULL)
312        {
313                err("no memory");
314                free(newnode);
315                return NULL;
316        }
317       
318        ret = sscanf(line, "%d#%[^#]", &newnode->type, name);
319        if(ret != 2)
320        {
321                if(count > 0)
322                {
323                        err("mediadbcategory line %d not ok or double", count);
324                }
325                else
326                {
327                        err("add mediadbcategory");
328                }
329                free(name);
330                free(newnode);
331                return NULL;
332        }
333       
334        if(newnode->type != type)
335        {
336                free(name);
337                free(newnode);
338                return NULL;   
339        }
340
341        newnode->name = ostrcat(name, NULL, 1, 0);
342
343        if(flag == 0) m_lock(&status.mediadbmutex, 17);
344        node = mediadbcategory;
345
346        if(last == NULL)
347        {
348                while(node != NULL && strcasecmp(newnode->name, node->name) > 0)
349                {
350                        prev = node;
351                        node = node->next;
352                }
353        }
354        else
355        {
356                prev = last;
357                node = last->next;
358        }
359
360        if(prev == NULL)
361                mediadbcategory = newnode;
362        else
363        {
364                prev->next = newnode;
365                newnode->prev = prev;
366        }
367        newnode->next = node;
368        if(node != NULL) node->prev = newnode;
369       
370        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
371        //debug(1000, "out");
372        return newnode;
373}
374
375int addmediadbcontent(struct mediadb* node, char *line, int len, int count)
376{
377        int ret = 0;
378        char* tmpstr = NULL, *type = NULL, *year = NULL, *rating = NULL;
379        char* votes = NULL, *timestamp = NULL, *flag = NULL;
380
381        if(node == NULL) return 1;
382
383        if(len > 0) tmpstr = malloc(len + 1);
384        if(tmpstr != NULL)
385        {
386                memcpy(tmpstr, line, len);
387                tmpstr[len] = '\0';
388
389                node->id = tmpstr;
390
391                while(tmpstr[0] != '\0')
392                {
393                        if(tmpstr[0] == '#')
394                        {
395                                tmpstr[0] = '\0';
396                                tmpstr++;
397                                switch(ret)
398                                {
399                                        case 0: type = tmpstr; break;
400                                        case 1: node->title = tmpstr; break;
401                                        case 2: year = tmpstr; break;
402                                        case 3: node->released = tmpstr; break;
403                                        case 4: node->runtime = tmpstr; break;
404                                        case 5: node->genre = tmpstr; break;
405                                        case 6: node->director = tmpstr; break;
406                                        case 7: node->writer = tmpstr; break;
407                                        case 8: node->actors = tmpstr; break;
408                                        case 9: node->plot = tmpstr; break;
409                                        case 10: node->poster = tmpstr; break;
410                                        case 11: rating = tmpstr; break;
411                                        case 12: votes = tmpstr; break;
412                                        case 13: node->path = tmpstr; break;
413                                        case 14: node->file = tmpstr; break;
414                                        case 15: timestamp = tmpstr; break;
415                                        case 16: flag = tmpstr; break;
416                                }
417
418                                ret++;
419                        }
420                        else
421                                tmpstr++;
422                }
423        }
424
425        if(ret != 17)
426        {
427                if(count > 0)
428                {
429                        err("mediadb line %d not ok (ret=%d)", count, ret);
430                }
431                else
432                {
433                        err("add mediadb (ret=%d)", ret);
434                }
435                freemediadbcontent(node);
436                return 1;
437        }
438
439        if(type != NULL) node->type = atoi(type);
440        if(year != NULL) node->year = atoi(year);
441        if(rating != NULL) node->rating = atoi(rating);
442        if(votes != NULL) node->votes = atoi(votes);
443        if(timestamp != NULL) node->timestamp = strtoul(timestamp, NULL, 10);
444        if(flag != NULL) node->flag = atoi(flag);
445
446        return 0;
447}
448
449//flag 0: with lock
450//flag 1: without lock
451struct mediadb* addmediadb(char *line, int len, int count, struct mediadb* last, int sort, int flag)
452{
453        //debug(1000, "in");
454        struct mediadb *newnode = NULL, *prev = NULL, *node = NULL;
455        int ret = 0;
456
457        if(line == NULL) return NULL;
458
459        newnode = (struct mediadb*)calloc(1, sizeof(struct mediadb));
460        if(newnode == NULL)
461        {
462                err("no memory");
463                return NULL;
464        }
465
466        ret = addmediadbcontent(newnode, line, len, count);
467        if(ret == 1)
468        {
469                free(newnode);
470                return NULL;
471        }
472
473        if(flag == 0) m_lock(&status.mediadbmutex, 17);
474        node = mediadb;
475
476        status.writemediadb = 1;
477        modifymediadbcache(newnode->path, newnode->file, newnode);
478
479        if(last == NULL)
480        {
481                if(sort == 1)
482                {
483                        while(node != NULL && strcasecmp(newnode->title, node->title) > 0)
484                        {
485                                prev = node;
486                                node = node->next;
487                        }
488                }
489                else
490                {
491                        while(node != NULL)
492                        {
493                                prev = node;
494                                node = node->next;
495                        }
496                }
497        }
498        else
499        {
500                prev = last;
501                node = last->next;
502        }
503
504        if(prev == NULL)
505                mediadb = newnode;
506        else
507        {
508                prev->next = newnode;
509                newnode->prev = prev;
510        }
511        newnode->next = node;
512        if(node != NULL) node->prev = newnode;
513
514        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
515        //debug(1000, "out");
516        return newnode;
517}
518
519struct mediadb* createmediadb(struct mediadb* update, char* id, int type, char* title, char* year, char* released, char* runtime, char* genre, char* director, char* writer, char* actors, char* plot, char* poster, char* rating, char* votes, char* path, char* file, int flag)
520{
521        struct mediadb* mnode = NULL;
522        char* tmpstr = NULL;
523
524        if(path == NULL || file == NULL) return NULL;
525        if(title == NULL) title = file;
526
527        id = stringreplacechar(id, '#', ' ');
528        title = stringreplacechar(title, '#', ' ');
529        year = stringreplacechar(year, '#', ' ');
530        released = stringreplacechar(released, '#', ' ');
531        runtime = stringreplacechar(runtime, '#', ' ');
532        genre = stringreplacechar(genre, '#', ' ');
533        director = stringreplacechar(director, '#', ' ');
534        writer = stringreplacechar(writer, '#', ' ');
535        actors = stringreplacechar(actors, '#', ' ');
536        plot = stringreplacechar(plot, '#', ' ');
537        poster = stringreplacechar(poster, '#', ' ');
538        rating = stringreplacechar(rating, ',', '.');
539        votes = stringreplacechar(votes, ',', '.');
540        path = stringreplacechar(path, '#', ' ');
541        file = stringreplacechar(file, '#', ' ');
542
543        tmpstr = ostrcat(tmpstr, id, 1, 0);
544        tmpstr = ostrcat(tmpstr, "#", 1, 0);
545        tmpstr = ostrcat(tmpstr, oitoa(type), 1, 1);
546        tmpstr = ostrcat(tmpstr, "#", 1, 0);
547        tmpstr = ostrcat(tmpstr, title, 1, 0);
548        tmpstr = ostrcat(tmpstr, "#", 1, 0);
549        if(year != NULL) tmpstr = ostrcat(tmpstr, oitoa(atoi(year)), 1, 1);
550        tmpstr = ostrcat(tmpstr, "#", 1, 0);
551        tmpstr = ostrcat(tmpstr, released, 1, 0);
552        tmpstr = ostrcat(tmpstr, "#", 1, 0);
553        tmpstr = ostrcat(tmpstr, runtime, 1, 0);
554        tmpstr = ostrcat(tmpstr, "#", 1, 0);
555        tmpstr = ostrcat(tmpstr, genre, 1, 0);
556        tmpstr = ostrcat(tmpstr, "#", 1, 0);
557        tmpstr = ostrcat(tmpstr, director, 1, 0);
558        tmpstr = ostrcat(tmpstr, "#", 1, 0);
559        tmpstr = ostrcat(tmpstr, writer, 1, 0);
560        tmpstr = ostrcat(tmpstr, "#", 1, 0);
561        tmpstr = ostrcat(tmpstr, actors, 1, 0);
562        tmpstr = ostrcat(tmpstr, "#", 1, 0);
563        tmpstr = ostrcat(tmpstr, plot, 1, 0);
564        tmpstr = ostrcat(tmpstr, "#", 1, 0);
565        tmpstr = ostrcat(tmpstr, poster, 1, 0);
566        tmpstr = ostrcat(tmpstr, "#", 1, 0);
567        if(rating != NULL) tmpstr = ostrcat(tmpstr, oitoa(atoi(rating)), 1, 1);
568        tmpstr = ostrcat(tmpstr, "#", 1, 0);
569        if(votes != NULL) tmpstr = ostrcat(tmpstr, oitoa(atoi(votes)), 1, 1);
570        tmpstr = ostrcat(tmpstr, "#", 1, 0);
571        tmpstr = ostrcat(tmpstr, path, 1, 0);
572        tmpstr = ostrcat(tmpstr, "#", 1, 0);
573        tmpstr = ostrcat(tmpstr, file, 1, 0);
574        tmpstr = ostrcat(tmpstr, "#", 1, 0);
575        tmpstr = ostrcat(tmpstr, olutoa(time(NULL)), 1, 1);
576        tmpstr = ostrcat(tmpstr, "#", 1, 0);
577        tmpstr = ostrcat(tmpstr, oitoa(flag), 1, 1);
578
579        if(update != NULL)
580        {
581                m_lock(&status.mediadbmutex, 17);
582                delmediadbcache(update->file, update);
583                freemediadbcontent(update);
584                addmediadbcontent(update, tmpstr, strlen(tmpstr), 1);
585                modifymediadbcache(update->path, update->file, update);
586                m_unlock(&status.mediadbmutex, 17);
587                mnode = update;
588        }
589        else
590                mnode = addmediadb(tmpstr, strlen(tmpstr), 1, NULL, 1, 0);
591
592        free(tmpstr);
593
594        return mnode;
595}
596
597//flag 0: read mediadb
598//flag 1: read mediadbcategory
599int readmediadb(const char* filename, int type, int flag)
600{
601        debug(1000, "in");
602        FILE *fd = NULL;
603        char *fileline = NULL;
604        int linecount = 0, len = 0;
605        struct mediadb* last = NULL, *tmplast = NULL;
606        struct mediadbcategory* lastcategory = NULL, *tmplastcategory = NULL;
607
608        m_lock(&status.mediadbmutex, 17);
609
610        if(flag == 0 && mediadb != NULL)
611        {
612                m_unlock(&status.mediadbmutex, 17);
613                return 1;
614        }
615
616        fileline = malloc(MINMALLOC);
617        if(fileline == NULL)
618        {
619                err("no memory");
620                m_unlock(&status.mediadbmutex, 17);
621                return 1;
622        }
623
624        fd = fopen(filename, "r");
625        if(fd == NULL)
626        {
627                perr("can't open %s", filename);
628                free(fileline);
629                m_unlock(&status.mediadbmutex, 17);
630                return 1;
631        }
632
633        while(fgets(fileline, MINMALLOC, fd) != NULL)
634        {
635                if(fileline[0] == '\n')
636                        continue;
637                len = strlen(fileline) - 1;
638                if(fileline[len] == '\n')
639                        fileline[len] = '\0';
640                if(fileline[len - 1] == '\r')
641                        fileline[len - 1] = '\0';
642
643                linecount++;
644
645                if(flag == 0)
646                {
647                        if(last == NULL) last = tmplast;
648                        last = addmediadb(fileline, len + 2, linecount, last, 0, 1);
649                        if(last != NULL) tmplast = last;
650                }
651                else
652                {
653                        if(lastcategory == NULL) lastcategory = tmplastcategory;
654                        lastcategory = addmediadbcategory(fileline, type, linecount, lastcategory, 1);
655                        if(lastcategory != NULL) tmplastcategory = lastcategory;
656                }
657        }
658
659        status.writemediadb = 0;
660
661        free(fileline);
662        fclose(fd);
663        m_unlock(&status.mediadbmutex, 17);
664        return 0;
665}
666
667void freemediadbcontent(struct mediadb* node)
668{
669        if(node == NULL) return;
670
671        free(node->id); node->id = NULL;
672        node->type = 0;
673        node->title = NULL;
674        node->year = 0;
675        node->released = NULL;
676        node->runtime = NULL;
677        node->genre = NULL;
678        node->director = NULL;
679        node->writer = NULL;
680        node->actors = NULL;
681        node->plot = NULL;
682        node->poster = NULL;
683        node->rating = 0;
684        node->votes = 0;
685        node->file = NULL;
686        node->timestamp = 0;
687        node->flag = 0;
688}
689
690//flag 0: with lock
691//flag 1: without lock
692int delmediadbfilter(struct mediadbfilter* mnode, int flag)
693{
694        debug(1000, "in");
695        int ret = 1;
696
697        if(flag == 0) m_lock(&status.mediadbmutex, 17);
698        struct mediadbfilter *node = mediadbfilter, *prev = mediadbfilter;
699
700        while(node != NULL)
701        {
702                if(node == mnode)
703                {
704                        ret = 0;
705                        if(node == mediadbfilter)
706                        {
707                                mediadbfilter = node->next;
708                                if(mediadbfilter != NULL)
709                                        mediadbfilter->prev = NULL;
710                        }
711                        else
712                        {
713                                prev->next = node->next;
714                                if(node->next != NULL)
715                                        node->next->prev = prev;
716                        }
717
718                        free(node);
719                        node = NULL;
720
721                        break;
722                }
723
724                prev = node;
725                node = node->next;
726        }
727
728        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
729        debug(1000, "out");
730        return ret;
731}
732
733int delmediadbcategory(struct mediadbcategory* mnode, int flag)
734{
735        debug(1000, "in");
736        int ret = 1;
737
738        m_lock(&status.mediadbmutex, 17);
739        struct mediadbcategory *node = mediadbcategory, *prev = mediadbcategory;
740
741        while(node != NULL)
742        {
743                if(node == mnode)
744                {
745                        ret = 0;
746                        if(node == mediadbcategory)
747                        {
748                                mediadbcategory = node->next;
749                                if(mediadbcategory != NULL)
750                                        mediadbcategory->prev = NULL;
751                        }
752                        else
753                        {
754                                prev->next = node->next;
755                                if(node->next != NULL)
756                                        node->next->prev = prev;
757                        }
758                       
759                        free(node->name);
760                        node->name = NULL;
761
762                        free(node);
763                        node = NULL;
764
765                        break;
766                }
767
768                prev = node;
769                node = node->next;
770        }
771
772        m_unlock(&status.mediadbmutex, 17);
773        debug(1000, "out");
774        return ret;
775}
776
777int delmediadb(struct mediadb* mnode, int flag)
778{
779        debug(1000, "in");
780        int ret = 1;
781
782        m_lock(&status.mediadbmutex, 17);
783        struct mediadb *node = mediadb, *prev = mediadb;
784
785        while(node != NULL)
786        {
787                if(node == mnode)
788                {
789                        ret = 0;
790                        status.writemediadb = 1;
791                        if(node == mediadb)
792                        {
793                                mediadb = node->next;
794                                if(mediadb != NULL)
795                                        mediadb->prev = NULL;
796                        }
797                        else
798                        {
799                                prev->next = node->next;
800                                if(node->next != NULL)
801                                        node->next->prev = prev;
802                        }
803
804                        struct mediadbfilter* mfnode = getmediadbfilter(node, 1);
805                        if(mfnode != NULL)
806                                delmediadbfilter(mfnode, 1);
807                        delmediadbcache(node->file, node);
808                        freemediadbcontent(node);
809
810                        free(node);
811                        node = NULL;
812
813                        break;
814                }
815
816                prev = node;
817                node = node->next;
818        }
819
820        m_unlock(&status.mediadbmutex, 17);
821        debug(1000, "out");
822        return ret;
823}
824
825void freemediadbfilter(int flag)
826{
827        debug(1000, "in");
828        struct mediadbfilter *node = mediadbfilter, *prev = mediadbfilter;
829
830        while(node != NULL)
831        {
832                prev = node;
833                node = node->next;
834                if(prev != NULL)
835                        delmediadbfilter(prev, flag);
836        }
837        debug(1000, "out");
838}
839
840void freemediadbcategory(int flag)
841{
842        debug(1000, "in");
843        struct mediadbcategory *node = mediadbcategory, *prev = mediadbcategory;
844
845        while(node != NULL)
846        {
847                prev = node;
848                node = node->next;
849                if(prev != NULL)
850                        delmediadbcategory(prev, flag);
851        }
852        debug(1000, "out");
853}
854
855void freemediadb(int flag)
856{
857        debug(1000, "in");
858        struct mediadb *node = mediadb, *prev = mediadb;
859
860        while(node != NULL)
861        {
862                prev = node;
863                node = node->next;
864                if(prev != NULL)
865                        delmediadb(prev, flag);
866        }
867
868        status.writemediadb = 0;
869        debug(1000, "out");
870}
871
872int writemediadbcategory(const char *filename)
873{
874        debug(1000, "in");
875        FILE *fd = NULL;
876        struct mediadbcategory *node = NULL;
877        int ret = 0;
878
879        fd = fopen(filename, "w");
880        if(fd == NULL)
881        {
882                perr("can't open %s", filename);
883                return 1;
884        }
885
886        m_lock(&status.mediadbmutex, 17);
887        node = mediadbcategory;
888
889        while(node != NULL)
890        {
891                ret = fprintf(fd, "%d#%s\n", node->type, node->name);
892                if(ret < 0)
893                {
894                        perr("writting file %s", filename);
895                }
896                node = node->next;
897        }
898
899        m_unlock(&status.mediadbmutex, 17);
900
901        fclose(fd);
902        debug(1000, "out");
903        return 0;
904}
905
906int writemediadb(const char *filename)
907{
908        debug(1000, "in");
909        FILE *fd = NULL;
910        struct mediadb *node = NULL;
911        int ret = 0;
912
913        fd = fopen(filename, "w");
914        if(fd == NULL)
915        {
916                perr("can't open %s", filename);
917                return 1;
918        }
919
920        m_lock(&status.mediadbmutex, 17);
921        node = mediadb;
922
923        while(node != NULL)
924        {
925                ret = fprintf(fd, "%s#%d#%s#%d#%s#%s#%s#%s#%s#%s#%s#%s#%d#%d#%s#%s#%lu#%d\n", node->id, node->type, node->title, node->year, node->released, node->runtime, node->genre, node->director, node->writer, node->actors, node->plot, node->poster, node->rating, node->votes, node->path, node->file, node->timestamp, node->flag);
926
927                if(ret < 0)
928                {
929                        perr("writting file %s", filename);
930                }
931                node = node->next;
932        }
933
934        m_unlock(&status.mediadbmutex, 17);
935
936        fclose(fd);
937        status.writemediadb = 0;
938        debug(1000, "out");
939        return 0;
940}
941
942//flag 0: all
943//flag 1: year
944//flag 2: director
945//flag 3: actors
946//flag 4: category
947//flag 5: rating
948//flag 6: genre
949//flag 7: a-z
950//flag 8: no imdb info
951int createmediadbfilter(int type, char* search, int flag)
952{
953        int isearch = 0, count = 0;
954        struct mediadb* node = mediadb;
955        struct mediadbfilter* last = NULL;
956
957        if(status.mediadbthreadstatus == 1) return 0;
958
959        if(flag == 1 || flag == 5)
960        {
961                if(search == NULL) return 0;
962                isearch = atoi(search);
963        }
964        if(flag == 7 && search == NULL) return 0;
965
966        freemediadbfilter(0);
967        while(node != NULL)
968        {
969                if(node->type == type)
970                {
971                        if(flag == 0)
972                        {
973                                last = addmediadbfilter(node, count, 1, last);
974                                count++;
975                        }
976                        else if(flag == 1 && node->year == isearch)
977                        {
978                                last = addmediadbfilter(node, count, 1, last);
979                                count++;
980                        }
981                        else if(flag == 2 && ostrstrcase(node->director, search) != NULL)
982                        {
983                                last = addmediadbfilter(node, count, 1, last);
984                                count++;
985                        }
986                        else if(flag == 3 && ostrstrcase(node->actors, search) != NULL)
987                        {
988                                last = addmediadbfilter(node, count, 1, last);
989                                count++;
990                        }
991                        else if(flag == 4 && ostrstrcase(node->file, search) != NULL)
992                        {
993                                last = addmediadbfilter(node, count, 1, last);
994                                count++;
995                        }
996                        else if(flag == 5 && node->rating == isearch)
997                        {
998                                last = addmediadbfilter(node, count, 1, last);
999                                count++;
1000                        }
1001                        else if(flag == 6 && ostrstrcase(node->genre, search) != NULL)
1002                        {
1003                                last = addmediadbfilter(node, count, 1, last);
1004                                count++;
1005                        }
1006                        else if(flag == 7 && node->title != NULL && node->title[0] == search[0])
1007                        {
1008                                last = addmediadbfilter(node, count, 1, last);
1009                                count++;
1010                        }
1011                        else if(flag == 8 && (node->id == NULL || strlen(node->id) == 0))
1012                        {
1013                                last = addmediadbfilter(node, count, 1, last);
1014                                count++;
1015                        }
1016                }
1017                node = node->next;
1018        }
1019
1020        return count;
1021}
1022
1023//flag: bit 31 = 0 (rekursive), 1 (no recursive)
1024void mediadbscanthread(struct stimerthread* self, char* path, int flag)
1025{
1026        struct mediadb *node = mediadb, *prev = mediadb;
1027        struct mediadbcategory *cnode = NULL;
1028        struct hdd *hddnode = NULL;
1029        struct splitstr* ret = NULL;
1030        char* tmpstr = NULL, *tmpsplit = NULL;
1031
1032        if(status.mediadbthread != NULL || self == NULL)
1033        {
1034                free(path); path = NULL;
1035                return;
1036        }
1037
1038        debug(777, "mediadb scanthread start");
1039        status.mediadbthreadstatus = 1;
1040        status.mediadbthread = self;
1041
1042        int type = flag;
1043        int onlydir = checkbit(flag, 31);
1044
1045        type = clearbit(type, 31);
1046
1047        if(type > 999)
1048        {
1049                type = type - 1000;
1050       
1051                char* tmpstr = NULL;
1052                tmpstr = ostrcat(tmpstr, _("MediaDB directory scan started in Background !"), 1, 0);
1053                tmpstr = ostrcat(tmpstr, "\n\n  ", 1, 0);
1054                tmpstr = ostrcat(tmpstr, _("Delete MediaDB before scan"), 1, 0);
1055                tmpstr = ostrcat(tmpstr, ": \t", 1, 0);
1056                if(ostrcmp(getconfig("mediadbscandelall", NULL), "1") == 0)
1057                        tmpstr = ostrcat(tmpstr, _("yes"), 1, 0);
1058                else
1059                        tmpstr = ostrcat(tmpstr, _("no"), 1, 0);
1060                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);                 
1061                tmpstr = ostrcat(tmpstr, _("Delete unused entrys before scan"), 1, 0);
1062                tmpstr = ostrcat(tmpstr, ": \t", 1, 0);         
1063                if(ostrcmp(getconfig("mediadbscandelnotfound", NULL), "1") == 0)
1064                        tmpstr = ostrcat(tmpstr, _("yes"), 1, 0);
1065                else
1066                        tmpstr = ostrcat(tmpstr, _("no"), 1, 0);
1067                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);
1068                tmpstr = ostrcat(tmpstr, _("scan Directory:"), 1, 0);
1069                tmpstr = ostrcat(tmpstr, " \t\t\t", 1, 0);
1070                tmpstr = ostrcat(tmpstr, path, 1, 0);
1071                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);         
1072                tmpstr = ostrcat(tmpstr, _("MediaDB place:"), 1, 0);
1073                tmpstr = ostrcat(tmpstr, " \t\t\t", 1, 0);                             
1074                tmpstr = ostrcat(tmpstr, getconfig("mediadbpath", NULL), 1, 0);
1075                int count = 0;
1076               
1077                while(count < 5)
1078                {
1079                        sleep(1);
1080                        count++;
1081                }
1082                textbox(_("Message"), tmpstr, _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 500, 10, 0);
1083                free(tmpstr), tmpstr = NULL;
1084                count = 0;
1085        }
1086       
1087        //clear all other db in mem
1088        freemediadbfilter(0);
1089
1090        if(getconfigint("mediadbscandelall", NULL) == 1)
1091        {
1092                delallfiles(getconfig("mediadbpath", NULL), ".jpg");
1093                delallfiles(getconfig("mediadbpath", NULL), ".mvi");
1094                freemediadb(0);
1095        }
1096        else
1097                readmediadb(getconfig("mediadbfile", NULL), 0, 0);
1098
1099        //check mediadb for not exist file
1100        if(getconfigint("mediadbscandelnotfound", NULL) == 1)
1101        {
1102                while(node != NULL)
1103                {
1104                        prev = node;
1105                        node = node->next;
1106                        tmpstr = ostrcat(prev->path, "/", 0, 0);
1107                        tmpstr = ostrcat(tmpstr, prev->file, 1, 0);
1108                        tmpstr = addmountpart(tmpstr, 1);
1109                        if(tmpstr == NULL)
1110                        {
1111                                //TODO: unlink all jpg to node
1112                                delmediadb(prev, 0);
1113                        }
1114                        free(tmpstr); tmpstr = NULL;
1115                }
1116        }
1117
1118        status.mediadbsavetime = time(NULL) + 60;
1119        status.mediadbthreadstatus = 2;
1120
1121        //find media files
1122        if(path == NULL)
1123        {
1124                findfiles("/media/usb", type, onlydir, 0, 1);
1125                findfiles("/media/net", type, onlydir, 0, 1);
1126                /*
1127                addhddall();
1128                hddnode = hdd;
1129
1130                while(hddnode != NULL)
1131                {
1132                        if(hddnode->partition != 0)
1133                        {
1134                                tmpstr = ostrcat("/autofs/", hddnode->device, 0, 0);
1135                                findfiles(tmpstr, type, onlydir, 0, 1);
1136                                free(tmpstr); tmpstr = NULL;
1137                        }
1138                        hddnode = hddnode->next;
1139                }
1140                */
1141        }
1142        else
1143                findfiles(path, type, onlydir, 0, 1);
1144
1145        free(path); path = NULL;
1146
1147        writemediadb(getconfig("mediadbfile", NULL));
1148
1149        status.mediadbthreadstatus = 3;
1150        sleep(3); //wait a little if other thread read category
1151
1152        freemediadbcategory(0);
1153
1154        //create year
1155        node = mediadb;
1156        while(node != NULL)
1157        {
1158                char* year = oitoa(node->year);
1159                int treffer = 1;
1160                cnode = mediadbcategory;
1161                while(cnode != NULL)
1162                {
1163                        if(ostrcmp(cnode->name, year) == 0)
1164                                treffer = 0;
1165                        cnode = cnode->next;
1166                }
1167
1168                if(treffer == 1)
1169                {
1170                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1171                        tmpstr = ostrcat(tmpstr, year, 1, 0);
1172                        debug(777, "add year %d", node->year);
1173                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1174                        free(tmpstr); tmpstr = NULL;
1175                }
1176
1177                free(year); year = NULL;
1178                node = node->next;
1179        }
1180        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".year", 0, 0);
1181        writemediadbcategory(tmpstr);
1182        free(tmpstr); tmpstr = NULL;
1183        freemediadbcategory(0);
1184
1185        //create director
1186        node = mediadb;
1187        while(node != NULL)
1188        {
1189                if(node->director == NULL)
1190                {
1191                        node = node->next;
1192                        continue;
1193                }
1194
1195                int treffer = 1;
1196                cnode = mediadbcategory;
1197                while(cnode != NULL)
1198                {
1199                        if(ostrcmp(cnode->name, node->director) == 0)
1200                                treffer = 0;
1201                        cnode = cnode->next;
1202                }
1203       
1204                        if(treffer == 1)
1205                {
1206                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1207                        tmpstr = ostrcat(tmpstr, node->director, 1, 0);
1208                        debug(777, "add director %s", node->director);
1209                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1210                        free(tmpstr); tmpstr = NULL;
1211                }
1212
1213                node = node->next;
1214        }
1215        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".director", 0, 0);
1216        writemediadbcategory(tmpstr);
1217        free(tmpstr); tmpstr = NULL;
1218        freemediadbcategory(0);
1219
1220        //create rating
1221        node = mediadb;
1222        while(node != NULL)
1223        {
1224                char* rating = oitoa(node->rating);
1225                int treffer = 1;
1226                cnode = mediadbcategory;
1227                while(cnode != NULL)
1228                {
1229                        if(ostrcmp(cnode->name, rating) == 0)
1230                                treffer = 0;
1231                        cnode = cnode->next;
1232                }
1233
1234                if(treffer == 1)
1235                {
1236                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1237                        tmpstr = ostrcat(tmpstr, rating, 1, 0);
1238                        debug(777, "add rating %d", node->rating);
1239                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1240                        free(tmpstr); tmpstr = NULL;
1241                }
1242
1243                free(rating); rating = NULL;
1244                node = node->next;
1245        }
1246        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".rating", 0, 0);
1247        writemediadbcategory(tmpstr);
1248        free(tmpstr); tmpstr = NULL;
1249        freemediadbcategory(0);
1250       
1251        //create actors
1252        node = mediadb;
1253        while(node != NULL)
1254        {
1255                if(node->actors == NULL)
1256                {
1257                        node = node->next;
1258                        continue;
1259                }
1260
1261                //split
1262                int i = 0, count = 0;
1263                tmpsplit = ostrcat(node->actors, NULL, 0, 0);
1264                ret = strsplit(tmpsplit, ",", &count);
1265               
1266                if(ret != NULL)
1267                {
1268                        for(i = 0; i < count; i++)
1269                        {
1270                                int treffer = 1;
1271                                strstrip((&ret[i])->part);
1272                                cnode = mediadbcategory;
1273                                while(cnode != NULL)
1274                                {
1275                                        if(ostrcmp(cnode->name, (&ret[i])->part) == 0)
1276                                                treffer = 0;
1277                                        cnode = cnode->next;
1278                                }
1279               
1280                                if(treffer == 1)
1281                                {
1282                                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1283                                        tmpstr = ostrcat(tmpstr, (&ret[i])->part, 1, 0);
1284                                        debug(777, "add actor %s", (&ret[i])->part);
1285                                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1286                                        free(tmpstr); tmpstr = NULL;
1287                                }
1288                        }
1289                }
1290
1291                free(tmpsplit); tmpsplit = NULL;
1292                free(ret); ret = NULL;
1293                node = node->next;
1294        }
1295        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".actors", 0, 0);
1296        writemediadbcategory(tmpstr);
1297        free(tmpstr); tmpstr = NULL;
1298        freemediadbcategory(0);
1299       
1300        //create category
1301        node = mediadb;
1302        while(node != NULL)
1303        {
1304                //split
1305                int i = 0, count = 0;
1306                tmpsplit = ostrcat(node->path, NULL, 0, 0);
1307                ret = strsplit(tmpsplit, "/", &count);
1308               
1309                if(ret != NULL)
1310                {
1311                        for(i = 0; i < count; i++)
1312                        {
1313                                int treffer = 1;
1314                                strstrip((&ret[i])->part);
1315                                cnode = mediadbcategory;
1316                                while(cnode != NULL)
1317                                {
1318                                        if(ostrcmp(cnode->name, (&ret[i])->part) == 0)
1319                                                treffer = 0;
1320                                        cnode = cnode->next;
1321                                }
1322               
1323                                if(treffer == 1)
1324                                {
1325                                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1326                                        tmpstr = ostrcat(tmpstr, (&ret[i])->part, 1, 0);
1327                                        debug(777, "add category %s", (&ret[i])->part);
1328                                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1329                                        free(tmpstr); tmpstr = NULL;
1330                                }
1331                        }
1332                }
1333
1334                free(tmpsplit); tmpsplit = NULL;
1335                free(ret); ret = NULL;
1336                node = node->next;
1337        }
1338        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".category", 0, 0);
1339        writemediadbcategory(tmpstr);
1340        free(tmpstr); tmpstr = NULL;
1341        freemediadbcategory(0);
1342
1343        //create genre
1344        node = mediadb;
1345        while(node != NULL)
1346        {
1347                if(node->genre == NULL)
1348                {
1349                        node = node->next;
1350                        continue;
1351                }
1352
1353                //split
1354                int i = 0, count = 0;
1355                tmpsplit = ostrcat(node->genre, NULL, 0, 0);
1356                ret = strsplit(tmpsplit, ",", &count);
1357               
1358                if(ret != NULL)
1359                {
1360                        for(i = 0; i < count; i++)
1361                        {               
1362                                int treffer = 1;
1363                                strstrip((&ret[i])->part);
1364                                cnode = mediadbcategory;
1365                                while(cnode != NULL)
1366                                {
1367                                        if(ostrcmp(cnode->name, (&ret[i])->part) == 0)
1368                                                treffer = 0;
1369                                        cnode = cnode->next;
1370                                }
1371               
1372                                if(treffer == 1)
1373                                {
1374                                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1375                                        tmpstr = ostrcat(tmpstr, (&ret[i])->part, 1, 0);
1376                                        debug(777, "add genre %s", (&ret[i])->part);
1377                                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1378                                        free(tmpstr); tmpstr = NULL;
1379                                }
1380                        }
1381                }
1382
1383                free(tmpsplit); tmpsplit = NULL;
1384                free(ret); ret = NULL;
1385                node = node->next;
1386        }
1387        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".genre", 0, 0);
1388        writemediadbcategory(tmpstr);
1389        free(tmpstr); tmpstr = NULL;
1390        freemediadbcategory(0);
1391
1392        //create az
1393        int i = 0;
1394        char* tmpbuf = malloc(2);
1395        if(tmpbuf != NULL)
1396        {
1397                for(i = 65; i < 91; i++)
1398                {
1399                        snprintf(tmpbuf, 2, "%c", i);
1400                        tmpstr = ostrcat("0#", tmpbuf, 0, 0);
1401                        addmediadbcategory(tmpstr, 0, 1, NULL, 0);
1402                        free(tmpstr); tmpstr = NULL;
1403                }
1404                for(i = 65; i < 91; i++)
1405                {
1406                        snprintf(tmpbuf, 2, "%c", i);
1407                        tmpstr = ostrcat("1#", tmpbuf, 0, 0);
1408                        addmediadbcategory(tmpstr, 1, 1, NULL, 0);
1409                        free(tmpstr); tmpstr = NULL;
1410                }
1411                for(i = 65; i < 91; i++)
1412                {
1413                        snprintf(tmpbuf, 2, "%c", i);
1414                        tmpstr = ostrcat("2#", tmpbuf, 0, 0);
1415                        addmediadbcategory(tmpstr, 2, 1, NULL, 0);
1416                        free(tmpstr); tmpstr = NULL;
1417                }
1418        }
1419        free(tmpbuf); tmpbuf = NULL;
1420        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".az", 0, 0);
1421        writemediadbcategory(tmpstr);
1422        free(tmpstr); tmpstr = NULL;
1423        freemediadbcategory(0);
1424
1425        status.mediadbthread = NULL;
1426        status.mediadbthreadstatus = 0;
1427        debug(777, "mediadb scanthread end");
1428}
1429
1430void mediadbfindfilecb(char* path, char* file, int type)
1431{
1432        char* shortpath = NULL, *tmpstr = NULL;
1433        struct mediadb *node = NULL;
1434       
1435        shortpath = delmountpart(path, 0);
1436        if(shortpath == NULL) return; //no mountpart found
1437
1438        m_lock(&status.mediadbmutex, 17);
1439        node = mediadb;
1440        //check if entry exist
1441        while(node != NULL)
1442        {
1443                if(ostrcmp(node->file, file) == 0)
1444                {
1445                        //check directory
1446                        if(ostrcmp(shortpath, node->path) == 0) //same file
1447                                break;
1448
1449                        //check file size
1450                        tmpstr = ostrcat(path, "/", 0, 0);
1451                        tmpstr = ostrcat(tmpstr, file, 1, 0);
1452                        off64_t s1 = getfilesize(tmpstr);
1453                        free(tmpstr); tmpstr = NULL;
1454                       
1455                        tmpstr = ostrcat(node->path, "/", 0, 0);
1456                        tmpstr = ostrcat(tmpstr, node->file, 1, 0);
1457                        tmpstr = addmountpart(tmpstr, 1);
1458                        off64_t s2 = getfilesize(tmpstr);
1459                        free(tmpstr); tmpstr = NULL;
1460                       
1461                        if(s1 == s2) //seems the same file
1462                                break;
1463                }
1464                node = node->next;
1465        }
1466        m_unlock(&status.mediadbmutex, 17);
1467
1468        int tout = getconfigint("mediadbscantimeout", NULL);
1469
1470        if(node == NULL || (node != NULL && checkbit(node->flag, 31) == 0 && tout == 0) || (node != NULL && checkbit(node->flag, 31) == 0 && time(NULL) > node->timestamp + (tout * 86400)))
1471        {
1472                if(type == 0)
1473                {
1474                        struct imdb* imdb = NULL;
1475                        struct imdbapi* imdbapi = NULL;
1476                        struct tmdb* tmdb = NULL;
1477
1478                        //create imdb search name
1479                        char* shortname = ostrcat(file, NULL, 0, 0);
1480                        string_tolower(shortname);
1481                        shortname = string_shortname(shortname, 1);
1482                        shortname = string_shortname(shortname, 2);
1483                        string_removechar(shortname);
1484                        strstrip(shortname);
1485
1486                        //got imdb infos
1487                        struct skin* imdbplugin = getplugin("IMDb");
1488                        if(imdbplugin != NULL)
1489                        {
1490                                struct imdb* (*startplugin)(struct imdb**, char*, int, int, int);
1491                                startplugin = dlsym(imdbplugin->pluginhandle, "getimdb");
1492                                if(startplugin != NULL)
1493                                        imdb = startplugin(&imdb, shortname, 0, 1, 0);
1494                        }
1495
1496                        struct skin* imdbapiplugin = getplugin("IMDb-API");
1497                        if(imdbplugin != NULL)
1498                        {
1499                                struct imdbapi* (*startplugin)(struct imdbapi**, char*, int, int);
1500                                startplugin = dlsym(imdbapiplugin->pluginhandle, "getimdbapi");
1501                                if(startplugin != NULL)
1502                                {
1503                                        if(imdb == NULL)
1504                                                imdbapi = startplugin(&imdbapi, shortname, 0, 1);
1505                                        else if(imdb->id != NULL)
1506                                                imdbapi = startplugin(&imdbapi, imdb->id, 1, 1);
1507                                }
1508                        }
1509
1510                        struct skin* tmdbplugin = NULL;
1511                        tmdbplugin = getplugin("TMDb");
1512                        if(tmdbplugin != NULL)
1513                        {
1514                                struct tmdb* (*startplugin)(struct tmdb**, char*, int, int);
1515                                startplugin = dlsym(tmdbplugin->pluginhandle, "gettmdb");
1516                                if(startplugin != NULL)
1517                                {
1518                                        if(imdb != NULL && imdb->id != NULL)
1519                                                tmdb = startplugin(&tmdb, imdb->id, 1, 1);
1520                                        else if(imdbapi != NULL && imdbapi->id != NULL)
1521                                                tmdb = startplugin(&tmdb, imdbapi->id, 1, 1);
1522                                }
1523                        }
1524
1525      debugimdbnode(imdb);
1526                       
1527                        if(imdb != NULL && tmdb != NULL)
1528                        {
1529                                if(imdb->id == NULL) imdb->id = ostrcat(imdb->id, tmdb->imdbid, 1, 0);                 
1530                                if(imdb->title == NULL) imdb->title = ostrcat(imdb->title, tmdb->title, 1, 0); 
1531                                if(imdb->genre == NULL) imdb->genre = ostrcat(imdb->genre, tmdb->genre, 1, 0);
1532//                              if(imdb->writer == NULL) imdb->writer = ostrcat(imdb->writer, tmdb->writer, 1, 0);
1533//                              if(imdb->director == NULL) imdb->director = ostrcat(imdb->director, tmdb->director, 1, 0);
1534//                              if(imdb->actors == NULL) imdb->actors = ostrcat(imdb->actors, tmdb->actors, 1, 0);
1535                                if(imdb->rating == NULL) imdb->rating = ostrcat(imdb->rating, tmdb->rating, 1, 0);
1536                                if(imdb->votes == NULL) imdb->votes = ostrcat(imdb->votes, tmdb->votes, 1, 0);
1537                                if(imdb->runtime == NULL) imdb->runtime = ostrcat(imdb->runtime, tmdb->runtime, 1, 0);
1538                                if(imdb->plot == NULL) imdb->plot = ostrcat(imdb->plot, tmdb->plot, 1, 0);
1539                                if(imdb->released == NULL) imdb->released = ostrcat(imdb->released, tmdb->released, 1, 0);
1540                                if(imdb->poster == NULL) imdb->poster = ostrcat(imdb->poster, tmdb->postermid, 1, 0);
1541                                if(imdb->thumb == NULL) imdb->thumb = ostrcat(imdb->thumb, tmdb->thumb, 1, 0);
1542                                if(imdb->year == NULL) imdb->year = ostrcat(imdb->year, tmdb->year, 1, 0);                             
1543                        }
1544
1545      debugimdbnode(imdb);
1546     
1547                        if(imdb != NULL && imdbapi != NULL)
1548                        {
1549                                if(imdb->id == NULL) imdb->id = ostrcat(imdb->id, imdbapi->id, 1, 0);                   
1550                                if(imdb->title == NULL) imdb->title = ostrcat(imdb->title, imdbapi->title, 1, 0);       
1551                                if(imdb->genre == NULL) imdb->genre = ostrcat(imdb->genre, imdbapi->genre, 1, 0);
1552                                if(imdb->writer == NULL) imdb->writer = ostrcat(imdb->writer, imdbapi->writer, 1, 0);
1553                                if(imdb->director == NULL) imdb->director = ostrcat(imdb->director, imdbapi->director, 1, 0);
1554                                if(imdb->actors == NULL) imdb->actors = ostrcat(imdb->actors, imdbapi->actors, 1, 0);
1555                                if(imdb->rating == NULL) imdb->rating = ostrcat(imdb->rating, imdbapi->rating, 1, 0);
1556                                if(imdb->votes == NULL) imdb->votes = ostrcat(imdb->votes, imdbapi->votes, 1, 0);
1557                                if(imdb->runtime == NULL) imdb->runtime = ostrcat(imdb->runtime, imdbapi->runtime, 1, 0);
1558                                if(imdb->plot == NULL) imdb->plot = ostrcat(imdb->plot, imdbapi->plot, 1, 0);
1559                                if(imdb->released == NULL) imdb->released = ostrcat(imdb->released, imdbapi->released, 1, 0);
1560                                if(imdb->poster == NULL) imdb->poster = ostrcat(imdb->poster, imdbapi->poster, 1, 0);
1561//                              if(imdb->thumb == NULL) imdb->thumb = ostrcat(imdb->thumb, imdbapi->thumb, 1, 0);
1562                                if(imdb->year == NULL) imdb->year = ostrcat(imdb->year, imdbapi->year, 1, 0);
1563                        }
1564
1565                        debug(777, "shortname: %s", shortname);
1566                        free(shortname); shortname = NULL;
1567                       
1568      debugimdbnode(imdb);
1569                       
1570                        debug(777, "add video: %s/%s", shortpath, file);
1571                        if(imdb != NULL)
1572                        {
1573                                debug(777, "imdb id %s", imdb->id);
1574                                createmediadb(node, imdb->id, type, imdb->title, imdb->year, imdb->released, imdb->runtime, imdb->genre, imdb->director, imdb->writer, imdb->actors, imdb->plot, imdb->id, imdb->rating, imdb->votes, shortpath, file, 0);
1575                        }
1576                        else
1577                                createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, 0);
1578
1579                        if(imdbplugin != NULL)
1580                        {
1581                                void (*startplugin)(struct imdb**, int flag);
1582                                startplugin = dlsym(imdbplugin->pluginhandle, "freeimdb");
1583                                if(startplugin != NULL)
1584                                        startplugin(&imdb, 0);
1585                        }
1586                        imdb = NULL;
1587                       
1588                        if(imdbapiplugin != NULL)
1589                        {
1590                                void (*startplugin)(struct imdbapi**, int);
1591                                startplugin = dlsym(imdbapiplugin->pluginhandle, "freeimdbapi");
1592                                if(startplugin != NULL)
1593                                        startplugin(&imdbapi, 0);
1594                        }
1595                        imdbapi = NULL;
1596
1597                        if(tmdbplugin != NULL)
1598                        {
1599                                void (*startplugin)(struct tmdb**, int);
1600                                startplugin = dlsym(tmdbplugin->pluginhandle, "freetmdb");
1601                                if(startplugin != NULL)
1602                                        startplugin(&tmdb, 0);
1603                        }
1604                        tmdb = NULL;
1605                }
1606                else if(type == 1)
1607                {
1608                        struct id3tag* id3tag = NULL;
1609
1610                        debug(777, "add audio: %s/%s", shortpath, file);
1611
1612                        char* tmpfile = ostrcat(path, "/", 0, 0);
1613                        tmpfile = ostrcat(tmpfile, file, 1, 0);
1614
1615                        int hash = gethash(tmpfile);
1616                        char* tmphash = olutoa(hash);
1617
1618                        id3tag = getid3(tmpfile, tmphash, 1);
1619
1620                        if(id3tag != NULL)
1621                        {
1622                                if(id3tag->poster != NULL)
1623                                        createmediadb(node, tmphash, type, id3tag->title, id3tag->year, NULL, NULL, id3tag->genretext, NULL, NULL, id3tag->artist, id3tag->album, tmphash, NULL, NULL, shortpath, file, 0);
1624                                else
1625                                        createmediadb(node, tmphash, type, id3tag->title, id3tag->year, NULL, NULL, id3tag->genretext, NULL, NULL, id3tag->artist, id3tag->album, NULL, NULL, NULL, shortpath, file, 0);
1626                        }
1627                        else
1628                                createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, 0);
1629
1630                        free(tmpfile); tmpfile = NULL;
1631                        free(tmphash); tmphash = NULL;
1632                        freeid3(id3tag); id3tag = NULL;
1633                }
1634                else if(type == 2)
1635                {
1636                        char* thumbfile = NULL;
1637
1638                        debug(777, "add pic: %s/%s", shortpath, file);
1639                        if(status.thumbthread != NULL)
1640                        {
1641                                //check if thumb exists
1642                                thumbfile = checkthumb(path, file);
1643                                if(thumbfile == NULL)
1644                                        addqueue(101, (void*)path, strlen(path) + 1, (void*)file, strlen(file) + 1, 0, NULL);
1645                        }
1646      free(thumbfile); thumbfile = NULL;
1647
1648                        createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, 0);
1649                }
1650        }
1651        free(shortpath); shortpath = NULL;
1652
1653        //save mediadb all 60 sek
1654        if(status.mediadbsavetime < time(NULL))
1655        {
1656                writemediadb(getconfig("mediadbfile", NULL));
1657                status.mediadbsavetime = time(NULL) + 60;
1658        }
1659}
1660
1661int findfiles(char* dirname, int type, int onlydir, int onlycount, int first)
1662{
1663        debug(777, "dir=%s type=%d onlydir=%d, onlycount=%d\n", dirname, type, onlydir, onlycount);
1664        DIR *d;
1665        char* tmpstr = NULL;
1666
1667        //Open the directory specified by dirname
1668        d = opendir(dirname);
1669
1670        //Check it was opened
1671        if(! d)
1672        {
1673                perr("Cannot open directory %s", dirname);
1674                return -1;
1675        }
1676
1677        int count = 0;
1678
1679        while(1)
1680        {
1681                struct dirent* entry;
1682                int path_length;
1683                char path[PATH_MAX];
1684
1685                snprintf(path, PATH_MAX, "%s", dirname);
1686                //Readdir gets subsequent entries from d
1687                entry = readdir(d);
1688
1689                if(!entry) //There are no more entries in this directory, so break out of the while loop
1690                        break;
1691
1692                //check if link is a dir
1693                if(first == 1 && entry->d_type == DT_LNK)
1694                {
1695                        tmpstr = createpath(path, entry->d_name);
1696                        if(isdir(tmpstr) == 1)
1697                                entry->d_type = DT_DIR;
1698
1699                        free(tmpstr); tmpstr = NULL;
1700                }
1701
1702                //See if entry is a subdirectory of d
1703                if(entry->d_type & DT_DIR)
1704                {
1705                        //Check that the directory is not d or d's parent
1706                        if(entry->d_name != NULL && entry->d_name[0] != '.' && entry->d_name[0] != '$')
1707                        {
1708                                path_length = snprintf(path, PATH_MAX, "%s/%s", dirname, entry->d_name);
1709                                if(path_length >= PATH_MAX)
1710                                {
1711                                        err("path length has got too long");
1712                                        return -1;
1713                                }
1714                                //Recursively call findfiles with the new path
1715                                if(onlydir == 0)
1716                                        findfiles(path, type, onlydir, onlycount, 0);
1717                        }
1718                }
1719                else //File
1720                {
1721                        //TODO: add extensions
1722                       
1723                        if((status.expertmodus > 0) || (file_exist("/var/swap/etc/.codecpack")))
1724                        {
1725                                if(!filelistflt(".avi .dat .divx .flv .mkv .m4v .mp4 .mov .mpg .mpeg .mts .m2ts .trp .ts .vdr .vob .wmv .rm", entry->d_name)) //video
1726                                {
1727                                        if(type == 0 || type == 100 || type == 90 || type == 91)
1728                                        {
1729                                                if(onlycount == 0)
1730                                                        mediadbfindfilecb(path, entry->d_name, 0);
1731                                                else
1732                                                        count += 1;
1733                                        }
1734                                }
1735                                else if(!filelistflt(".mp3 .flac .ogg .wma .ra", entry->d_name)) //audio
1736                                {
1737                                        if(type == 1 || type == 100 || type == 90 || type == 92)
1738                                        {
1739                                                if(onlycount == 0)
1740                                                        mediadbfindfilecb(path, entry->d_name, 1);
1741                                                else
1742                                                        count += 1;
1743                                        }
1744                                }
1745                                else if(!filelistflt(".jpg .png", entry->d_name)) //picture
1746                                {
1747                                        if(type == 2 || type == 100 || type == 91 || type == 92)
1748                                        {
1749                                                if(onlycount == 0)
1750                                                        mediadbfindfilecb(path, entry->d_name, 2);
1751                                                else
1752                                                        count += 1;
1753                                        }
1754                                }
1755                        }
1756                        else
1757                        {                       
1758                                if(!filelistflt(".avi .mkv .mpg .mpeg .ts", entry->d_name)) //video
1759                                {
1760                                        if(type == 0 || type == 100 || type == 90 || type == 91)
1761                                        {
1762                                                if(onlycount == 0)
1763                                                        mediadbfindfilecb(path, entry->d_name, 0);
1764                                                else
1765                                                        count += 1;
1766                                        }
1767                                }
1768                                else if(!filelistflt(".mp3 .flac .ogg", entry->d_name)) //audio
1769                                {
1770                                        if(type == 1 || type == 100 || type == 90 || type == 92)
1771                                        {
1772                                                if(onlycount == 0)
1773                                                        mediadbfindfilecb(path, entry->d_name, 1);
1774                                                else
1775                                                        count += 1;
1776                                        }
1777                                }
1778                                else if(!filelistflt(".jpg .png", entry->d_name)) //picture
1779                                {
1780                                        if(type == 2 || type == 100 || type == 91 || type == 92)
1781                                        {
1782                                                if(onlycount == 0)
1783                                                        mediadbfindfilecb(path, entry->d_name, 2);
1784                                                else
1785                                                        count += 1;
1786                                        }
1787                                }
1788                        }
1789                }
1790        }
1791
1792        //After going through all the entries, close the directory
1793        if(closedir(d))
1794        {
1795                perr("Could not close %s", dirname);
1796                return -1;
1797        }
1798
1799        if(onlycount == 1)
1800                return count;
1801        return 0;
1802}
1803
1804//type 0=video, 1=audio, 2=pic, 90=video/audio, 91=video/pic, 92=audio/pic, 100=all
1805//flag: 0 = scan recursive
1806//flag: 1 = not scan recursive
1807void mediadbscan(char* path, int type, int flag)
1808{
1809        int count = 0;
1810
1811        if(flag == 1) type = type | 0x80000000;
1812
1813        //param1 (path) is freed in thread
1814        addtimer(&mediadbscanthread, START, 1000, 1, (void*)ostrcat(path, NULL, 0, 0), (void*)type, NULL);
1815
1816        //block a little
1817        while(status.mediadbthread != NULL && count < 20)
1818        {
1819                usleep(100000);
1820                count++;
1821        }
1822}
1823
1824#endif
Note: See TracBrowser for help on using the repository browser.