source: titan/titan/mediadb.h @ 16991

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

[titan] add random funktion

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