source: titan/titan/mediadb.h @ 30678

Last change on this file since 30678 was 28733, checked in by obi, 10 years ago

[Wichtig/important] add auto secretfeed and codecpack for Community User. add community_user and community_pass to menu > settings(Einstellungen) > adjust(erweitert)

File size: 71.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                err("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                err("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        return newnode;
289}
290
291//flag 0: with lock
292//flag 1: without lock
293struct mediadbcategory* addmediadbcategory(char* line, int type, int count, struct mediadbcategory* last, int flag)
294{
295        struct mediadbcategory *newnode = NULL, *prev = NULL, *node = NULL;
296        char* name = NULL;
297        int ret = 0;
298
299        if(line == NULL) return NULL;
300
301        newnode = (struct mediadbcategory*)calloc(1, sizeof(struct mediadbcategory));
302        if(newnode == NULL)
303        {
304                err("no memory");
305                return NULL;
306        }
307       
308        name = malloc(256);
309        if(name == NULL)
310        {
311                err("no memory");
312                free(newnode);
313                return NULL;
314        }
315       
316        ret = sscanf(line, "%d#%[^#]", &newnode->type, name);
317        if(ret != 2)
318        {
319                if(count > 0)
320                {
321                        err("mediadbcategory line %d not ok or double", count);
322                }
323                else
324                {
325                        err("add mediadbcategory");
326                }
327                free(name);
328                free(newnode);
329                return NULL;
330        }
331       
332        if(newnode->type != type)
333        {
334                free(name);
335                free(newnode);
336                return NULL;   
337        }
338
339        newnode->name = ostrshrink(name);
340
341        if(flag == 0) m_lock(&status.mediadbmutex, 17);
342        node = mediadbcategory;
343
344        if(last == NULL)
345        {
346                while(node != NULL && strcasecmp(newnode->name, node->name) > 0)
347                {
348                        prev = node;
349                        node = node->next;
350                }
351        }
352        else
353        {
354                prev = last;
355                node = last->next;
356        }
357
358        if(prev == NULL)
359                mediadbcategory = newnode;
360        else
361        {
362                prev->next = newnode;
363                newnode->prev = prev;
364        }
365        newnode->next = node;
366        if(node != NULL) node->prev = newnode;
367       
368        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
369        return newnode;
370}
371
372int addmediadbcontent(struct mediadb* node, char *line, int len, int count)
373{
374        int ret = 0;
375        char* tmpstr = NULL, *type = NULL, *year = NULL, *rating = NULL;
376        char* votes = NULL, *timestamp = NULL, *flag = NULL, *backdropcount = NULL;
377
378        if(node == NULL) return 1;
379
380        if(len > 0) tmpstr = malloc(len + 1);
381        if(tmpstr != NULL)
382        {
383                memcpy(tmpstr, line, len);
384                tmpstr[len] = '\0';
385
386                node->id = tmpstr;
387
388                while(tmpstr[0] != '\0')
389                {
390                        if(tmpstr[0] == '#')
391                        {
392                                tmpstr[0] = '\0';
393                                tmpstr++;
394                                switch(ret)
395                                {
396                                        case 0: type = tmpstr; break;
397                                        case 1: node->title = tmpstr; break;
398                                        case 2: year = tmpstr; break;
399                                        case 3: node->released = tmpstr; break;
400                                        case 4: node->runtime = tmpstr; break;
401                                        case 5: node->genre = tmpstr; break;
402                                        case 6: node->director = tmpstr; break;
403                                        case 7: node->writer = tmpstr; break;
404                                        case 8: node->actors = tmpstr; break;
405                                        case 9: node->plot = tmpstr; break;
406                                        case 10: node->poster = tmpstr; break;
407                                        case 11: rating = tmpstr; break;
408                                        case 12: votes = tmpstr; break;
409                                        case 13: node->path = tmpstr; break;
410                                        case 14: node->file = tmpstr; break;
411                                        case 15: node->shortname = tmpstr; break;
412                                        case 16: node->fileinfo = tmpstr; break;       
413                                        case 17: timestamp = tmpstr; break;
414                                        case 18: flag = tmpstr; break;
415                                        case 19: backdropcount = tmpstr; break;
416                                }
417
418                                ret++;
419                        }
420                        else
421                                tmpstr++;
422                }
423        }
424
425        if(ret != 20)
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        if(backdropcount != NULL) node->backdropcount = atoi(backdropcount);
446
447        return 0;
448}
449
450//flag 0: with lock
451//flag 1: without lock
452struct mediadb* addmediadb(char *line, int len, int count, struct mediadb* last, int sort, int flag)
453{
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        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, char* shortname, char* fileinfo, int flag, int backdropcount)
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        shortname = stringreplacechar(shortname, '#', ' ');
542        fileinfo = stringreplacechar(fileinfo, '#', ' ');
543       
544        tmpstr = ostrcat(tmpstr, id, 1, 0);
545        tmpstr = ostrcat(tmpstr, "#", 1, 0);
546        tmpstr = ostrcat(tmpstr, oitoa(type), 1, 1);
547        tmpstr = ostrcat(tmpstr, "#", 1, 0);
548        tmpstr = ostrcat(tmpstr, title, 1, 0);
549        tmpstr = ostrcat(tmpstr, "#", 1, 0);
550        if(year != NULL) tmpstr = ostrcat(tmpstr, oitoa(atoi(year)), 1, 1);
551        tmpstr = ostrcat(tmpstr, "#", 1, 0);
552        tmpstr = ostrcat(tmpstr, released, 1, 0);
553        tmpstr = ostrcat(tmpstr, "#", 1, 0);
554        tmpstr = ostrcat(tmpstr, runtime, 1, 0);
555        tmpstr = ostrcat(tmpstr, "#", 1, 0);
556        tmpstr = ostrcat(tmpstr, genre, 1, 0);
557        tmpstr = ostrcat(tmpstr, "#", 1, 0);
558        tmpstr = ostrcat(tmpstr, director, 1, 0);
559        tmpstr = ostrcat(tmpstr, "#", 1, 0);
560        tmpstr = ostrcat(tmpstr, writer, 1, 0);
561        tmpstr = ostrcat(tmpstr, "#", 1, 0);
562        tmpstr = ostrcat(tmpstr, actors, 1, 0);
563        tmpstr = ostrcat(tmpstr, "#", 1, 0);
564        tmpstr = ostrcat(tmpstr, plot, 1, 0);
565        tmpstr = ostrcat(tmpstr, "#", 1, 0);
566        tmpstr = ostrcat(tmpstr, poster, 1, 0);
567        tmpstr = ostrcat(tmpstr, "#", 1, 0);
568        if(rating != NULL) tmpstr = ostrcat(tmpstr, oitoa(atoi(rating)), 1, 1);
569        tmpstr = ostrcat(tmpstr, "#", 1, 0);
570        if(votes != NULL) tmpstr = ostrcat(tmpstr, oitoa(atoi(votes)), 1, 1);
571        tmpstr = ostrcat(tmpstr, "#", 1, 0);
572        tmpstr = ostrcat(tmpstr, path, 1, 0);
573        tmpstr = ostrcat(tmpstr, "#", 1, 0);
574        tmpstr = ostrcat(tmpstr, file, 1, 0);
575        tmpstr = ostrcat(tmpstr, "#", 1, 0);
576        tmpstr = ostrcat(tmpstr, shortname, 1, 0);
577        tmpstr = ostrcat(tmpstr, "#", 1, 0);
578        tmpstr = ostrcat(tmpstr, fileinfo, 1, 0);
579        tmpstr = ostrcat(tmpstr, "#", 1, 0);
580        tmpstr = ostrcat(tmpstr, olutoa(time(NULL)), 1, 1);
581        tmpstr = ostrcat(tmpstr, "#", 1, 0);
582        tmpstr = ostrcat(tmpstr, oitoa(flag), 1, 1);
583        tmpstr = ostrcat(tmpstr, "#", 1, 0);
584        tmpstr = ostrcat(tmpstr, oitoa(backdropcount), 1, 1);
585
586        tmpstr = string_replace_all("\n", "", tmpstr, 1);
587        tmpstr = string_replace_all("\r", "", tmpstr, 1);
588
589        if(update != NULL)
590        {
591                m_lock(&status.mediadbmutex, 17);
592                delmediadbcache(update->file, update);
593                freemediadbcontent(update);
594                addmediadbcontent(update, tmpstr, strlen(tmpstr), 1);
595                modifymediadbcache(update->path, update->file, update);
596                m_unlock(&status.mediadbmutex, 17);
597                mnode = update;
598        }
599        else
600                mnode = addmediadb(tmpstr, strlen(tmpstr), 1, NULL, 1, 0);
601
602        free(tmpstr);
603
604        return mnode;
605}
606
607//flag 0: read mediadb
608//flag 1: read mediadbcategory
609int readmediadb(const char* filename, int type, int flag)
610{
611        FILE *fd = NULL;
612        char *fileline = NULL;
613        int linecount = 0, len = 0;
614        struct mediadb* last = NULL, *tmplast = NULL;
615        struct mediadbcategory* lastcategory = NULL, *tmplastcategory = NULL;
616
617        m_lock(&status.mediadbmutex, 17);
618
619        if(flag == 0 && mediadb != NULL)
620        {
621                m_unlock(&status.mediadbmutex, 17);
622                return 1;
623        }
624
625        fileline = malloc(MINMALLOC);
626        if(fileline == NULL)
627        {
628                err("no memory");
629                m_unlock(&status.mediadbmutex, 17);
630                return 1;
631        }
632
633        fd = fopen(filename, "r");
634        if(fd == NULL)
635        {
636                perr("can't open %s", filename);
637                free(fileline);
638                m_unlock(&status.mediadbmutex, 17);
639                return 1;
640        }
641
642        while(fgets(fileline, MINMALLOC, fd) != NULL)
643        {
644                if(fileline[0] == '\n')
645                        continue;
646                len = strlen(fileline) - 1;
647                if(fileline[len] == '\n')
648                        fileline[len] = '\0';
649                if(fileline[len - 1] == '\r')
650                        fileline[len - 1] = '\0';
651
652                linecount++;
653
654                if(flag == 0)
655                {
656                        if(last == NULL) last = tmplast;
657                        last = addmediadb(fileline, len + 2, linecount, last, 0, 1);
658                        if(last != NULL) tmplast = last;
659                }
660                else
661                {
662                        if(lastcategory == NULL) lastcategory = tmplastcategory;
663                        lastcategory = addmediadbcategory(fileline, type, linecount, lastcategory, 1);
664                        if(lastcategory != NULL) tmplastcategory = lastcategory;
665                }
666        }
667
668        status.writemediadb = 0;
669
670        free(fileline);
671        fclose(fd);
672        m_unlock(&status.mediadbmutex, 17);
673        return 0;
674}
675
676void freemediadbcontent(struct mediadb* node)
677{
678        if(node == NULL) return;
679
680        free(node->id); node->id = NULL;
681        node->type = 0;
682        node->title = NULL;
683        node->year = 0;
684        node->released = NULL;
685        node->runtime = NULL;
686        node->genre = NULL;
687        node->director = NULL;
688        node->writer = NULL;
689        node->actors = NULL;
690        node->plot = NULL;
691        node->poster = NULL;
692        node->rating = 0;
693        node->votes = 0;
694        node->file = NULL;
695        node->shortname = NULL;
696        node->fileinfo = NULL;
697        node->timestamp = 0;
698        node->flag = 0;
699        node->backdropcount = 0;
700}
701
702//flag 0: with lock
703//flag 1: without lock
704int delmediadbfilter(struct mediadbfilter* mnode, int flag)
705{
706        int ret = 1;
707
708        if(flag == 0) m_lock(&status.mediadbmutex, 17);
709        struct mediadbfilter *node = mediadbfilter, *prev = mediadbfilter;
710
711        while(node != NULL)
712        {
713                if(node == mnode)
714                {
715                        ret = 0;
716                        if(node == mediadbfilter)
717                        {
718                                mediadbfilter = node->next;
719                                if(mediadbfilter != NULL)
720                                        mediadbfilter->prev = NULL;
721                        }
722                        else
723                        {
724                                prev->next = node->next;
725                                if(node->next != NULL)
726                                        node->next->prev = prev;
727                        }
728
729                        free(node);
730                        node = NULL;
731
732                        break;
733                }
734
735                prev = node;
736                node = node->next;
737        }
738
739        if(flag == 0) m_unlock(&status.mediadbmutex, 17);
740        return ret;
741}
742
743int delmediadbcategory(struct mediadbcategory* mnode, int flag)
744{
745        int ret = 1;
746
747        m_lock(&status.mediadbmutex, 17);
748        struct mediadbcategory *node = mediadbcategory, *prev = mediadbcategory;
749
750        while(node != NULL)
751        {
752                if(node == mnode)
753                {
754                        ret = 0;
755                        if(node == mediadbcategory)
756                        {
757                                mediadbcategory = node->next;
758                                if(mediadbcategory != NULL)
759                                        mediadbcategory->prev = NULL;
760                        }
761                        else
762                        {
763                                prev->next = node->next;
764                                if(node->next != NULL)
765                                        node->next->prev = prev;
766                        }
767                       
768                        free(node->name);
769                        node->name = NULL;
770
771                        free(node);
772                        node = NULL;
773
774                        break;
775                }
776
777                prev = node;
778                node = node->next;
779        }
780
781        m_unlock(&status.mediadbmutex, 17);
782        return ret;
783}
784
785int delmediadb(struct mediadb* mnode, int flag)
786{
787        int ret = 1;
788
789        m_lock(&status.mediadbmutex, 17);
790        struct mediadb *node = mediadb, *prev = mediadb;
791
792        while(node != NULL)
793        {
794                if(node == mnode)
795                {
796                        ret = 0;
797                        status.writemediadb = 1;
798                        if(node == mediadb)
799                        {
800                                mediadb = node->next;
801                                if(mediadb != NULL)
802                                        mediadb->prev = NULL;
803                        }
804                        else
805                        {
806                                prev->next = node->next;
807                                if(node->next != NULL)
808                                        node->next->prev = prev;
809                        }
810
811                        struct mediadbfilter* mfnode = getmediadbfilter(node, 1);
812                        if(mfnode != NULL)
813                                delmediadbfilter(mfnode, 1);
814                        delmediadbcache(node->file, node);
815                        freemediadbcontent(node);
816
817                        free(node);
818                        node = NULL;
819
820                        break;
821                }
822
823                prev = node;
824                node = node->next;
825        }
826
827        m_unlock(&status.mediadbmutex, 17);
828        return ret;
829}
830
831void freemediadbfilter(int flag)
832{
833        struct mediadbfilter *node = mediadbfilter, *prev = mediadbfilter;
834
835        while(node != NULL)
836        {
837                prev = node;
838                node = node->next;
839                if(prev != NULL)
840                        delmediadbfilter(prev, flag);
841        }
842}
843
844void freemediadbcategory(int flag)
845{
846        struct mediadbcategory *node = mediadbcategory, *prev = mediadbcategory;
847
848        while(node != NULL)
849        {
850                prev = node;
851                node = node->next;
852                if(prev != NULL)
853                        delmediadbcategory(prev, flag);
854        }
855}
856
857void freemediadb(int flag)
858{
859        struct mediadb *node = mediadb, *prev = mediadb;
860
861        while(node != NULL)
862        {
863                prev = node;
864                node = node->next;
865                if(prev != NULL)
866                        delmediadb(prev, flag);
867        }
868
869        status.writemediadb = 0;
870}
871
872int writemediadbcategory(const char *filename)
873{
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        return 0;
902}
903
904int writemediadb(const char *filename, struct mediadb* cmediadb)
905{
906        FILE *fd = NULL;
907        struct mediadb *node = NULL;
908        int ret = 0;
909
910        if(cmediadb == NULL)
911                fd = fopen(filename, "w");
912        else
913                fd = fopen(filename, "a");
914        if(fd == NULL)
915        {
916                perr("can't open %s", filename);
917                return 1;
918        }
919
920        int run = 0, videocount = 0, audiocount = 0, picturecount = 0;
921        if(status.mediadbfiles > 0)
922        {
923                getmediadbcounts(&videocount, &audiocount, &picturecount);
924                run = 1;
925        }
926
927        m_lock(&status.mediadbmutex, 17);
928        if(cmediadb == NULL)
929                node = mediadb;
930        else
931                node = cmediadb;
932
933        while(node != NULL)
934        {
935                ret = fprintf(fd, "%s#%d#%s#%d#%s#%s#%s#%s#%s#%s#%s#%s#%d#%d#%s#%s#%s#%s#%lu#%d#%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->shortname, node->fileinfo, node->timestamp, node->flag, node->backdropcount);
936
937                if(ret < 0)
938                {
939                        perr("writting file %s", filename);
940                }
941                if(cmediadb != NULL) break;
942                node = node->next;
943        }
944
945        if(run == 1 && status.mediadbfiles > 0)
946        {       
947                char* tmpstr = NULL;
948                tmpstr = ostrcat(tmpstr, "add(", 1, 0);
949                tmpstr = ostrcat(tmpstr, oitoa(videocount), 1, 1);
950                tmpstr = ostrcat(tmpstr, "/", 1, 0);
951                tmpstr = ostrcat(tmpstr, oitoa(status.mediadbfiles), 1, 1);
952                tmpstr = ostrcat(tmpstr, ")", 1, 0);
953                writevfdmenu(tmpstr);
954                free(tmpstr),tmpstr = NULL;
955        }
956
957        m_unlock(&status.mediadbmutex, 17);
958
959        fclose(fd);
960        status.writemediadb = 0;
961        return 0;
962}
963
964//flag 0: all
965//flag 1: year
966//flag 2: director
967//flag 3: actors
968//flag 4: category
969//flag 5: rating
970//flag 6: genre
971//flag 7: a-z
972//flag 8: no imdb info
973int createmediadbfilter(int type, char* search, int flag)
974{
975        int isearch = 0, count = 0;
976        struct mediadb* node = mediadb;
977        struct mediadbfilter* last = NULL;
978
979        if(status.mediadbthreadstatus == 1) return 0;
980
981        if(flag == 1 || flag == 5)
982        {
983                if(search == NULL) return 0;
984                isearch = atoi(search);
985        }
986        if(flag == 7 && search == NULL) return 0;
987
988        freemediadbfilter(0);
989        while(node != NULL)
990        {
991                if(node->type == type)
992                {
993                        if(flag == 0)
994                        {
995                                last = addmediadbfilter(node, count, 1, last);
996                                count++;
997                        }
998                        else if(flag == 1 && node->year == isearch)
999                        {
1000                                last = addmediadbfilter(node, count, 1, last);
1001                                count++;
1002                        }
1003                        else if(flag == 2 && ostrstrcase(node->director, search) != NULL)
1004                        {
1005                                last = addmediadbfilter(node, count, 1, last);
1006                                count++;
1007                        }
1008                        else if(flag == 3 && ostrstrcase(node->actors, search) != NULL)
1009                        {
1010                                last = addmediadbfilter(node, count, 1, last);
1011                                count++;
1012                        }
1013                        else if(flag == 4 && ostrstrcase(node->file, search) != NULL)
1014                        {
1015                                last = addmediadbfilter(node, count, 1, last);
1016                                count++;
1017                        }
1018                        else if(flag == 5 && node->rating == isearch)
1019                        {
1020                                last = addmediadbfilter(node, count, 1, last);
1021                                count++;
1022                        }
1023                        else if(flag == 6 && ostrstrcase(node->genre, search) != NULL)
1024                        {
1025                                last = addmediadbfilter(node, count, 1, last);
1026                                count++;
1027                        }
1028                        else if(flag == 7 && node->title != NULL && node->title[0] == search[0])
1029                        {
1030                                last = addmediadbfilter(node, count, 1, last);
1031                                count++;
1032                        }
1033                        else if(flag == 8 && (node->id == NULL || strlen(node->id) == 0))
1034                        {
1035                                last = addmediadbfilter(node, count, 1, last);
1036                                count++;
1037                        }
1038                }
1039                node = node->next;
1040        }
1041
1042        return count;
1043}
1044
1045//flag: bit 31 = 0 (rekursive), 1 (no recursive)
1046void mediadbscanthread(struct stimerthread* self, char* path, int flag)
1047{
1048        struct mediadb *node = mediadb, *prev = mediadb;
1049        struct mediadbcategory *cnode = NULL;
1050        //struct hdd *hddnode = NULL;
1051        struct splitstr* ret = NULL;
1052        char* tmpstr = NULL, *tmpsplit = NULL;
1053
1054        if(status.mediadbthread != NULL || self == NULL)
1055        {
1056                free(path); path = NULL;
1057                return;
1058        }
1059        debug(777, "mediadb scanthread start");
1060        status.mediadbthreadstatus = 1;
1061        status.mediadbthread = self;
1062
1063        int type = flag;
1064        int onlydir = checkbit(flag, 31);
1065
1066        type = clearbit(type, 31);
1067
1068        if(type > 999)
1069        {
1070                type = type - 1000;
1071       
1072                char* tmpstr = NULL;
1073                tmpstr = ostrcat(tmpstr, "scanning (", 1, 0);
1074                tmpstr = ostrcat(tmpstr, "0", 1, 0);
1075                tmpstr = ostrcat(tmpstr, "/", 1, 0);
1076                tmpstr = ostrcat(tmpstr, oitoa(status.mediadbfiles), 1, 1);
1077                tmpstr = ostrcat(tmpstr, ")", 1, 0);
1078                tmpstr = ostrcat(tmpstr, _("MediaDB directory scan started in Background !"), 1, 0);
1079                tmpstr = ostrcat(tmpstr, "\n\n  ", 1, 0);
1080                tmpstr = ostrcat(tmpstr, _("Delete MediaDB before scan"), 1, 0);
1081                tmpstr = ostrcat(tmpstr, ": \t", 1, 0);
1082                if(ostrcmp(getconfig("mediadbscandelall", NULL), "1") == 0)
1083                        tmpstr = ostrcat(tmpstr, _("yes"), 1, 0);
1084                else
1085                        tmpstr = ostrcat(tmpstr, _("no"), 1, 0);
1086                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);                 
1087                tmpstr = ostrcat(tmpstr, _("Delete unused entrys before scan"), 1, 0);
1088                tmpstr = ostrcat(tmpstr, ": \t", 1, 0);         
1089                if(ostrcmp(getconfig("mediadbscandelnotfound", NULL), "1") == 0)
1090                        tmpstr = ostrcat(tmpstr, _("yes"), 1, 0);
1091                else
1092                        tmpstr = ostrcat(tmpstr, _("no"), 1, 0);
1093                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);
1094                tmpstr = ostrcat(tmpstr, _("scan Directory"), 1, 0);
1095                tmpstr = ostrcat(tmpstr, ": \t\t\t", 1, 0);
1096                tmpstr = ostrcat(tmpstr, path, 1, 0);
1097                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);         
1098                tmpstr = ostrcat(tmpstr, _("MediaDB place"), 1, 0);
1099                tmpstr = ostrcat(tmpstr, ": \t\t\t", 1, 0);                             
1100                tmpstr = ostrcat(tmpstr, getconfig("mediadbpath", NULL), 1, 0);
1101                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);         
1102                tmpstr = ostrcat(tmpstr, _("MediaDB Debug"), 1, 0);
1103                tmpstr = ostrcat(tmpstr, ": \t\t\t", 1, 0);
1104                if(ostrcmp(getconfig("mediadbdebug", NULL), "1") == 0)
1105                        tmpstr = ostrcat(tmpstr, _("yes"), 1, 0);
1106                else
1107                        tmpstr = ostrcat(tmpstr, _("no"), 1, 0);
1108                tmpstr = ostrcat(tmpstr, "\n  ", 1, 0);         
1109                tmpstr = ostrcat(tmpstr, _("Backdrop Download Count"), 1, 0);
1110                tmpstr = ostrcat(tmpstr, ": \t\t", 1, 0);
1111                if(getconfigint("mediadbbackdrop", NULL) == 0)
1112                        tmpstr = ostrcat(tmpstr, _("all"), 1, 0);
1113                else
1114                        tmpstr = ostrcat(tmpstr, oitoa(getconfigint("mediadbbackdrop", NULL)), 1, 1);
1115
1116                int count = 0;
1117
1118                while(count < 60)
1119                {
1120                        sleep(1);
1121                        count++;
1122                }
1123
1124                if(!file_exist("/tmp/.autoscan"))
1125                {
1126                        free(path); path = NULL;
1127                        return;
1128                }
1129                                                                       
1130                textbox(_("Message"), tmpstr, _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 1100, 500, 10, 0);
1131                free(tmpstr), tmpstr = NULL;
1132                writevfdmenu("iMDB start");
1133                count = 0;
1134        }
1135       
1136        //clear all other db in mem
1137        freemediadbfilter(0);
1138
1139        if(getconfigint("mediadbscandelall", NULL) == 1)
1140        {
1141                delallfiles(getconfig("mediadbpath", NULL), ".jpg");
1142                delallfiles(getconfig("mediadbpath", NULL), ".mvi");
1143                freemediadb(0);
1144        }
1145        else
1146                readmediadb(getconfig("mediadbfile", NULL), 0, 0);
1147
1148        node = mediadb;
1149        prev = mediadb;
1150
1151        //check mediadb for not exist file
1152        if(getconfigint("mediadbscandelnotfound", NULL) == 1)
1153        {
1154                while(node != NULL)
1155                {
1156                        prev = node;
1157                        node = node->next;
1158                        tmpstr = ostrcat(prev->path, "/", 0, 0);
1159                        tmpstr = ostrcat(tmpstr, prev->file, 1, 0);
1160                        tmpstr = addmountpart(tmpstr, 1);
1161                        if(tmpstr == NULL)
1162                        {
1163                                //TODO: unlink all jpg to node
1164                                delmediadb(prev, 0);
1165                        }
1166                        free(tmpstr); tmpstr = NULL;
1167                }
1168        }
1169
1170        status.mediadbsavetime = 0;
1171        status.mediadbthreadstatus = 2;
1172
1173        //find media files
1174        if(path == NULL)
1175        {
1176                findfiles("/media/usb", type, onlydir, 0, 1);
1177                findfiles("/media/net", type, onlydir, 0, 1);
1178                /*
1179                addhddall();
1180                hddnode = hdd;
1181
1182                while(hddnode != NULL)
1183                {
1184                        if(hddnode->partition != 0)
1185                        {
1186                                tmpstr = ostrcat("/autofs/", hddnode->device, 0, 0);
1187                                findfiles(tmpstr, type, onlydir, 0, 1);
1188                                free(tmpstr); tmpstr = NULL;
1189                        }
1190                        hddnode = hddnode->next;
1191                }
1192                */
1193        }
1194        else
1195                findfiles(path, type, onlydir, 0, 1);
1196
1197        free(path); path = NULL;
1198
1199        writemediadb(getconfig("mediadbfile", NULL), NULL);
1200
1201        status.mediadbthreadstatus = 3;
1202        sleep(3); //wait a little if other thread read category
1203
1204        freemediadbcategory(0);
1205
1206        //create year
1207        node = mediadb;
1208        while(node != NULL)
1209        {
1210                char* year = oitoa(node->year);
1211                int treffer = 1;
1212                cnode = mediadbcategory;
1213                while(cnode != NULL)
1214                {
1215                        if(ostrcmp(cnode->name, year) == 0)
1216                                treffer = 0;
1217                        cnode = cnode->next;
1218                }
1219
1220                if(treffer == 1)
1221                {
1222                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1223                        tmpstr = ostrcat(tmpstr, year, 1, 0);
1224                        debug(777, "add year %d", node->year);
1225                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1226                        free(tmpstr); tmpstr = NULL;
1227                }
1228
1229                free(year); year = NULL;
1230                node = node->next;
1231        }
1232        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".year", 0, 0);
1233        writemediadbcategory(tmpstr);
1234        free(tmpstr); tmpstr = NULL;
1235        freemediadbcategory(0);
1236
1237        //create director
1238        node = mediadb;
1239        while(node != NULL)
1240        {
1241                if(node->director == NULL)
1242                {
1243                        node = node->next;
1244                        continue;
1245                }
1246
1247                int treffer = 1;
1248                cnode = mediadbcategory;
1249                while(cnode != NULL)
1250                {
1251                        if(ostrcmp(cnode->name, node->director) == 0)
1252                                treffer = 0;
1253                        cnode = cnode->next;
1254                }
1255       
1256                        if(treffer == 1)
1257                {
1258                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1259                        tmpstr = ostrcat(tmpstr, node->director, 1, 0);
1260                        debug(777, "add director %s", node->director);
1261                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1262                        free(tmpstr); tmpstr = NULL;
1263                }
1264
1265                node = node->next;
1266        }
1267        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".director", 0, 0);
1268        writemediadbcategory(tmpstr);
1269        free(tmpstr); tmpstr = NULL;
1270        freemediadbcategory(0);
1271
1272        //create rating
1273        node = mediadb;
1274        while(node != NULL)
1275        {
1276                char* rating = oitoa(node->rating);
1277                int treffer = 1;
1278                cnode = mediadbcategory;
1279                while(cnode != NULL)
1280                {
1281                        if(ostrcmp(cnode->name, rating) == 0)
1282                                treffer = 0;
1283                        cnode = cnode->next;
1284                }
1285
1286                if(treffer == 1)
1287                {
1288                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1289                        tmpstr = ostrcat(tmpstr, rating, 1, 0);
1290                        debug(777, "add rating %d", node->rating);
1291                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1292                        free(tmpstr); tmpstr = NULL;
1293                }
1294
1295                free(rating); rating = NULL;
1296                node = node->next;
1297        }
1298        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".rating", 0, 0);
1299        writemediadbcategory(tmpstr);
1300        free(tmpstr); tmpstr = NULL;
1301        freemediadbcategory(0);
1302       
1303        //create actors
1304        node = mediadb;
1305        while(node != NULL)
1306        {
1307                if(node->actors == NULL)
1308                {
1309                        node = node->next;
1310                        continue;
1311                }
1312
1313                //split
1314                int i = 0, count = 0;
1315                tmpsplit = ostrcat(node->actors, NULL, 0, 0);
1316                ret = strsplit(tmpsplit, ",", &count);
1317               
1318                if(ret != NULL)
1319                {
1320                        for(i = 0; i < count; i++)
1321                        {
1322                                int treffer = 1;
1323                                strstrip((&ret[i])->part);
1324                                cnode = mediadbcategory;
1325                                while(cnode != NULL)
1326                                {
1327                                        if(ostrcmp(cnode->name, (&ret[i])->part) == 0)
1328                                                treffer = 0;
1329                                        cnode = cnode->next;
1330                                }
1331               
1332                                if(treffer == 1)
1333                                {
1334                                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1335                                        tmpstr = ostrcat(tmpstr, (&ret[i])->part, 1, 0);
1336                                        debug(777, "add actor %s", (&ret[i])->part);
1337                                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1338                                        free(tmpstr); tmpstr = NULL;
1339                                }
1340                        }
1341                }
1342
1343                free(tmpsplit); tmpsplit = NULL;
1344                free(ret); ret = NULL;
1345                node = node->next;
1346        }
1347        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".actors", 0, 0);
1348        writemediadbcategory(tmpstr);
1349        free(tmpstr); tmpstr = NULL;
1350        freemediadbcategory(0);
1351       
1352        //create category
1353        node = mediadb;
1354        while(node != NULL)
1355        {
1356                //split
1357                int i = 0, count = 0;
1358                tmpsplit = ostrcat(node->path, NULL, 0, 0);
1359                ret = strsplit(tmpsplit, "/", &count);
1360               
1361                if(ret != NULL)
1362                {
1363                        for(i = 0; i < count; i++)
1364                        {
1365                                int treffer = 1;
1366                                strstrip((&ret[i])->part);
1367                                cnode = mediadbcategory;
1368                                while(cnode != NULL)
1369                                {
1370                                        if(ostrcmp(cnode->name, (&ret[i])->part) == 0)
1371                                                treffer = 0;
1372                                        cnode = cnode->next;
1373                                }
1374               
1375                                if(treffer == 1)
1376                                {
1377                                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1378                                        tmpstr = ostrcat(tmpstr, (&ret[i])->part, 1, 0);
1379                                        debug(777, "add category %s", (&ret[i])->part);
1380                                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1381                                        free(tmpstr); tmpstr = NULL;
1382                                }
1383                        }
1384                }
1385
1386                free(tmpsplit); tmpsplit = NULL;
1387                free(ret); ret = NULL;
1388                node = node->next;
1389        }
1390        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".category", 0, 0);
1391        writemediadbcategory(tmpstr);
1392        free(tmpstr); tmpstr = NULL;
1393        freemediadbcategory(0);
1394
1395        //create genre
1396        node = mediadb;
1397        while(node != NULL)
1398        {
1399                if(node->genre == NULL)
1400                {
1401                        node = node->next;
1402                        continue;
1403                }
1404
1405                //split
1406                int i = 0, count = 0;
1407                tmpsplit = ostrcat(node->genre, NULL, 0, 0);
1408                ret = strsplit(tmpsplit, ",", &count);
1409               
1410                if(ret != NULL)
1411                {
1412                        for(i = 0; i < count; i++)
1413                        {               
1414                                int treffer = 1;
1415                                strstrip((&ret[i])->part);
1416                                cnode = mediadbcategory;
1417                                while(cnode != NULL)
1418                                {
1419                                        if(ostrcmp(cnode->name, (&ret[i])->part) == 0)
1420                                                treffer = 0;
1421                                        cnode = cnode->next;
1422                                }
1423               
1424                                if(treffer == 1)
1425                                {
1426                                        tmpstr = ostrcat(oitoa(node->type), "#", 1, 0);
1427                                        tmpstr = ostrcat(tmpstr, (&ret[i])->part, 1, 0);
1428                                        debug(777, "add genre %s", (&ret[i])->part);
1429                                        addmediadbcategory(tmpstr, node->type, 1, NULL, 0);
1430                                        free(tmpstr); tmpstr = NULL;
1431                                }
1432                        }
1433                }
1434
1435                free(tmpsplit); tmpsplit = NULL;
1436                free(ret); ret = NULL;
1437                node = node->next;
1438        }
1439        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".genre", 0, 0);
1440        writemediadbcategory(tmpstr);
1441        free(tmpstr); tmpstr = NULL;
1442        freemediadbcategory(0);
1443
1444        //create az
1445        int i = 0;
1446        char* tmpbuf = malloc(2);
1447        if(tmpbuf != NULL)
1448        {
1449                for(i = 65; i < 91; i++)
1450                {
1451                        snprintf(tmpbuf, 2, "%c", i);
1452                        tmpstr = ostrcat("0#", tmpbuf, 0, 0);
1453                        addmediadbcategory(tmpstr, 0, 1, NULL, 0);
1454                        free(tmpstr); tmpstr = NULL;
1455                }
1456                for(i = 65; i < 91; i++)
1457                {
1458                        snprintf(tmpbuf, 2, "%c", i);
1459                        tmpstr = ostrcat("1#", tmpbuf, 0, 0);
1460                        addmediadbcategory(tmpstr, 1, 1, NULL, 0);
1461                        free(tmpstr); tmpstr = NULL;
1462                }
1463                for(i = 65; i < 91; i++)
1464                {
1465                        snprintf(tmpbuf, 2, "%c", i);
1466                        tmpstr = ostrcat("2#", tmpbuf, 0, 0);
1467                        addmediadbcategory(tmpstr, 2, 1, NULL, 0);
1468                        free(tmpstr); tmpstr = NULL;
1469                }
1470        }
1471        free(tmpbuf); tmpbuf = NULL;
1472        tmpstr = ostrcat(getconfig("mediadbfile", NULL), ".az", 0, 0);
1473        writemediadbcategory(tmpstr);
1474        free(tmpstr); tmpstr = NULL;
1475        freemediadbcategory(0);
1476
1477        textbox(_("Message"), _("MediaDB scan finished"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 10, 0);
1478        writevfdmenu("iMDB Done !");
1479
1480        status.mediadbsavetime = 0;
1481        status.mediadbfiles = 0;
1482        status.mediadbthread = NULL;
1483        status.mediadbthreadstatus = 0;
1484        debug(777, "mediadb scanthread end");
1485}
1486
1487// flag 1 = shortname
1488// flag 2 = fileinfo
1489char* createshortname(char* file, int *isrec, int *iscam, int flag)
1490{
1491        char* fileinfo = NULL, *shortname = NULL, *tmpstr = NULL;
1492        //create imdb search name
1493        shortname = ostrcat(file, NULL, 0, 0);
1494
1495        // create filelist info
1496        tmpstr = ostrcat(tmpstr, file, 1, 0);
1497        char* tmpstr1 = oregex(".*([0-9]{14,14}).*", tmpstr);
1498        if(tmpstr1 != NULL)
1499        {
1500                shortname = string_replace(tmpstr1, "", shortname, 1);
1501                if(fileinfo != NULL)
1502                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1503                fileinfo = ostrcat(fileinfo, "rec", 1, 0);
1504                *isrec = 1;
1505        }                       
1506        free(tmpstr1); tmpstr1 = NULL;
1507
1508        tmpstr1 = oregex(".*([0-9]{8,8}.*[0-9]{4,4}).*", tmpstr);
1509        if(tmpstr1 != NULL && *isrec == 0)
1510        {
1511                shortname = string_replace(tmpstr1, "", shortname, 1);
1512                if(fileinfo != NULL)
1513                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1514                fileinfo = ostrcat(fileinfo, "recold", 1, 0);
1515                *isrec = 1;
1516        }
1517        free(tmpstr1); tmpstr1 = NULL;
1518
1519        tmpstr1 = oregex(".*([0-9]{5,5}).*", tmpstr);
1520        if(tmpstr1 != NULL && *isrec == 0)
1521        {
1522                *iscam = 1;
1523                if(fileinfo != NULL)
1524                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1525                fileinfo = ostrcat(fileinfo, "cam", 1, 0);
1526        }
1527        free(tmpstr1), tmpstr1 = NULL;
1528       
1529        if(*isrec == 0 && *iscam == 0)
1530        {
1531                string_tolower(shortname);
1532                shortname = string_shortname(shortname, 2);
1533                string_removechar(shortname);
1534                strstrip(shortname);
1535        }
1536        else
1537        {
1538                char* cut = ostrcat(".", getfilenameext(tmpstr), 0, 1);
1539                cut = ostrcat(cut, "\0", 1, 0);
1540                shortname = string_replace(cut, "\0", shortname, 1);
1541                free(cut); cut = NULL;
1542
1543                string_removechar(shortname);
1544                strstrip(shortname);
1545        }
1546
1547        string_tolower(tmpstr);
1548
1549        if((ostrstr(tmpstr, ".special.extended.edition.") != NULL) || (ostrstr(tmpstr, ".sse.") != NULL))
1550        {
1551                if(fileinfo != NULL)
1552                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1553                fileinfo = ostrcat(fileinfo, "SEE-Version", 1, 0);
1554                shortname = string_replace(".special.extended.edition.", "", shortname, 1);
1555        }
1556        if((ostrstr(tmpstr, ".extended.version.") != NULL) || (ostrstr(tmpstr, ".extended.") != NULL) || (ostrstr(tmpstr, ".ed.") != NULL))
1557        {
1558                if(fileinfo != NULL)
1559                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1560                fileinfo = ostrcat(fileinfo, "ED-Version", 1, 0);
1561                shortname = string_replace(".extended.version.", "", shortname, 1);
1562        }
1563        if((ostrstr(tmpstr, ".uncut.") != NULL) || (ostrstr(tmpstr, ".uc.") != NULL))
1564        {
1565                if(fileinfo != NULL)
1566                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1567                fileinfo = ostrcat(fileinfo, "UC-Version", 1, 0);
1568        }
1569        if((ostrstr(tmpstr, ".unrated.") != NULL) || (ostrstr(tmpstr, ".ur.") != NULL))
1570        {
1571                if(fileinfo != NULL)
1572                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1573                fileinfo = ostrcat(fileinfo, "UR-Version", 1, 0);
1574        }
1575        if((ostrstr(tmpstr, ".telesync.") != NULL) || (ostrstr(tmpstr, ".ts.") != NULL))
1576        {
1577                if(fileinfo != NULL)
1578                        fileinfo = ostrcat(fileinfo, " ", 1, 0);                       
1579                fileinfo = ostrcat(fileinfo, "telesync", 1, 0);
1580        }
1581        if((ostrstr(tmpstr, ".telecine.") != NULL) || (ostrstr(tmpstr, ".tc.") != NULL))
1582        {
1583                if(fileinfo != NULL)
1584                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1585                fileinfo = ostrcat(fileinfo, "telecine", 1, 0);
1586        }
1587        if((ostrstr(tmpstr, ".dts.") != NULL) || (ostrstr(tmpstr, ".dtshd.") != NULL))
1588        {
1589                if(fileinfo != NULL)
1590                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1591                fileinfo = ostrcat(fileinfo, "dts", 1, 0);
1592        }
1593        if((ostrstr(tmpstr, ".ac3.") != NULL) || (ostrstr(tmpstr, ".ac3d.") != NULL) || (ostrstr(tmpstr, ".ac3hd.") != NULL))
1594        {
1595                if(fileinfo != NULL)
1596                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1597                fileinfo = ostrcat(fileinfo, "ac3", 1, 0);
1598        }
1599        if(ostrstr(tmpstr, ".r5.") != NULL)
1600        {
1601                if(fileinfo != NULL)
1602                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1603                fileinfo = ostrcat(fileinfo, "r5", 1, 0);
1604        }
1605        if(ostrstr(tmpstr, ".r3.") != NULL)
1606        {
1607                if(fileinfo != NULL)
1608                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1609                fileinfo = ostrcat(fileinfo, "r3", 1, 0);
1610        }
1611        if(ostrstr(tmpstr, ".r1.") != NULL)
1612        {
1613                if(fileinfo != NULL)
1614                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1615                fileinfo = ostrcat(fileinfo, "r1", 1, 0);
1616        }                       
1617        if(ostrstr(tmpstr, ".sample.") != NULL)
1618        {
1619                if(fileinfo != NULL)
1620                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1621                fileinfo = ostrcat(fileinfo, "sample", 1, 0);
1622        }
1623
1624        tmpstr1 = oregex(".*([0-9]{4,4}).*", tmpstr);
1625        if(tmpstr1 != NULL && *isrec == 0 && *iscam == 0)
1626        {
1627                if(fileinfo != NULL)
1628                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1629                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1630        }
1631        free(tmpstr1); tmpstr1 = NULL;
1632       
1633        tmpstr1 = oregex(".*(cd[0-9]{1,3}).*", tmpstr);
1634        if(tmpstr1 != NULL)
1635        {
1636                if(fileinfo != NULL)
1637                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1638                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1639                shortname = string_replace(tmpstr1, "", shortname, 1);
1640        }
1641        free(tmpstr1); tmpstr1 = NULL;
1642
1643        tmpstr1 = oregex(".*(dvd[0-9]{1,2}).*", tmpstr);
1644        if(tmpstr1 != NULL)
1645        {
1646                if(fileinfo != NULL)
1647                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1648                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1649                shortname = string_replace(tmpstr1, "", shortname, 1);
1650        }
1651        free(tmpstr1); tmpstr1 = NULL;
1652
1653        tmpstr1 = oregex(".*(s[0-9]{1,2}e[0-9]{1,2}e[0-9]{1,2}).*", tmpstr);
1654        if(tmpstr1 != NULL)
1655        {
1656                if(fileinfo != NULL)
1657                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1658                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1659                shortname = string_replace(tmpstr1, "", shortname, 1);
1660        }
1661        free(tmpstr1); tmpstr1 = NULL;
1662
1663        tmpstr1 = oregex(".*(s[0-9]{1,2}e[0-9]{1,2}).*", tmpstr);
1664        if(tmpstr1 != NULL)
1665        {
1666                if(fileinfo != NULL)
1667                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1668                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1669                shortname = string_replace(tmpstr1, "", shortname, 1);
1670        }
1671        free(tmpstr1); tmpstr1 = NULL;
1672
1673        tmpstr1 = oregex(".*(disc[0-9]{1,2}).*", tmpstr);
1674        if(tmpstr1 != NULL)
1675        {
1676                if(fileinfo != NULL)
1677                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1678                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1679                shortname = string_replace(tmpstr1, "", shortname, 1);
1680        }
1681        free(tmpstr1); tmpstr1 = NULL;
1682
1683        tmpstr1 = oregex(".*(season[0-9]{1,2}).*", tmpstr);
1684        if(tmpstr1 != NULL)
1685        {
1686                if(fileinfo != NULL)
1687                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1688                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1689                shortname = string_replace(tmpstr1, "", shortname, 1);
1690        }
1691        free(tmpstr1); tmpstr1 = NULL;
1692
1693        tmpstr1 = oregex(".*(episode[0-9]{1,2}).*", tmpstr);
1694        if(tmpstr1 != NULL)
1695        {
1696                if(fileinfo != NULL)
1697                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1698                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1699                shortname = string_replace(tmpstr1, "", shortname, 1);
1700        }
1701        free(tmpstr1); tmpstr1 = NULL;
1702
1703        tmpstr1 = oregex(".*(staffel[0-9]{1,2}).*", tmpstr);
1704        if(tmpstr1 != NULL)
1705        {
1706                if(fileinfo != NULL)
1707                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1708                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1709                shortname = string_replace(tmpstr1, "", shortname, 1);
1710        }
1711        free(tmpstr1); tmpstr1 = NULL;
1712
1713        tmpstr1 = oregex(".*(part[0-9]{1,2}).*", tmpstr);
1714        if(tmpstr1 != NULL)
1715        {
1716                if(fileinfo != NULL)
1717                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1718                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1719                shortname = string_replace(tmpstr1, "", shortname, 1);
1720        }
1721        free(tmpstr1); tmpstr1 = NULL;
1722
1723        strstrip(shortname);
1724
1725        if(fileinfo != NULL)
1726                fileinfo = ostrcat(fileinfo, " ", 1, 0);
1727        fileinfo = ostrcat(fileinfo, getfilenameext(tmpstr), 1, 1);
1728        free(tmpstr); tmpstr = NULL;
1729
1730        if(flag == 1)
1731        {
1732                free(fileinfo);
1733                return shortname;
1734        }
1735        else if(flag == 2)
1736        {
1737                free(shortname);
1738                return fileinfo;
1739        }
1740
1741        free(fileinfo);
1742        free(shortname);
1743        return NULL;
1744}
1745
1746struct mediadb* mediadbcheckfile(char* file, char* path, char* shortpath)
1747{
1748        char* tmpstr = NULL;
1749        struct mediadb *node = NULL;
1750
1751        if(file == NULL || path == NULL || shortpath == NULL) return NULL;
1752        m_lock(&status.mediadbmutex, 17);
1753
1754        //check if entry exist
1755        node = mediadb;
1756        while(node != NULL)
1757        {
1758                if(ostrcmp(node->file, file) == 0)
1759                {
1760                        //check directory
1761                        if(ostrcmp(shortpath, node->path) == 0) //same file
1762                                break;
1763
1764                        //check file size
1765                        tmpstr = ostrcat(path, "/", 0, 0);
1766                        tmpstr = ostrcat(tmpstr, file, 1, 0);
1767                        off64_t s1 = getfilesize(tmpstr);
1768                        free(tmpstr); tmpstr = NULL;
1769
1770                        tmpstr = ostrcat(node->path, "/", 0, 0);
1771                        tmpstr = ostrcat(tmpstr, node->file, 1, 0);
1772                        tmpstr = addmountpart(tmpstr, 1);
1773                        off64_t s2 = getfilesize(tmpstr);
1774                        free(tmpstr); tmpstr = NULL;
1775
1776                        if(s1 == s2) //seems the same file
1777                                break;
1778                }
1779                node = node->next;
1780        }
1781        m_unlock(&status.mediadbmutex, 17);
1782
1783        return node;
1784}
1785
1786int mediadbffmpeg1(char* file, char* path, char* timestamp, char* logfile)
1787{
1788        char* cmd = NULL;
1789
1790        if(file == NULL || path == NULL || timestamp == NULL) return 1;
1791
1792        cmd = ostrcat(cmd, "ffmpeg -i \"", 1, 0);
1793        cmd = ostrcat(cmd, path, 1, 0);
1794        cmd = ostrcat(cmd, "/", 1, 0);
1795        cmd = ostrcat(cmd, file, 1, 0);
1796//cmd = ostrcat(cmd, "\" -vframes 1 -s 1920x1080 ", 1, 0);
1797        cmd = ostrcat(cmd, "\" -vframes 1 -s 1280x720 ", 1, 0);
1798        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1799        cmd = ostrcat(cmd, "/", 1, 0);
1800        cmd = ostrcat(cmd, timestamp, 1, 0);
1801        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
1802
1803        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1804        {
1805                filedebug(logfile, "#############\nLocalfile: %s/%s_backdrop1.jpg\n#############", getconfig("mediadbpath", NULL), timestamp);
1806                cmd = ostrcat(cmd, " >> ", 1, 0);
1807                cmd = ostrcat(cmd, logfile, 1, 0);
1808                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1809        }
1810        else
1811                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1812
1813        debug(133, "cmd %s", cmd);
1814        system(cmd);
1815        free(cmd); cmd = NULL;
1816
1817        return 0;
1818}
1819
1820int mediadbffmpeg2(char* file, char* path, char* timestamp, char* logfile)
1821{
1822        char* cmd = NULL;
1823
1824        if(file == NULL || path == NULL || timestamp == NULL) return 1;
1825
1826        cmd = ostrcat(cmd, "ffmpeg -i \"", 1, 0);
1827        cmd = ostrcat(cmd, path, 1, 0);
1828        cmd = ostrcat(cmd, "/", 1, 0);
1829        cmd = ostrcat(cmd, file, 1, 0);
1830        cmd = ostrcat(cmd, "\" -vframes 1 -s 160x120 ", 1, 0);
1831        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1832        cmd = ostrcat(cmd, "/", 1, 0);
1833        cmd = ostrcat(cmd, timestamp, 1, 0);
1834        cmd = ostrcat(cmd, "_thumb.jpg", 1, 0);
1835
1836        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1837        {
1838                filedebug(logfile, "#############\nLocalfile: %s/%s_thumb.jpg\n#############", getconfig("mediadbpath", NULL), timestamp);
1839                cmd = ostrcat(cmd, " >> ", 1, 0);
1840                cmd = ostrcat(cmd, logfile, 1, 0);
1841                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1842        }
1843        else
1844                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1845
1846        debug(133, "cmd %s", cmd);
1847        system(cmd);
1848        free(cmd); cmd = NULL;
1849
1850        return 0;
1851}
1852
1853int mediadbffmpeg3(char* file, char* path, char* timestamp, char* logfile)
1854{
1855        char* cmd = NULL;
1856
1857        if(file == NULL || path == NULL || timestamp == NULL) return 1;
1858
1859        cmd = ostrcat(cmd, "ffmpeg -i \"", 1, 0);
1860        cmd = ostrcat(cmd, path, 1, 0);
1861        cmd = ostrcat(cmd, "/", 1, 0);
1862        cmd = ostrcat(cmd, file, 1, 0);
1863        cmd = ostrcat(cmd, "\" -vframes 1 -s 500x400 ", 1, 0);
1864        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1865        cmd = ostrcat(cmd, "/", 1, 0);
1866        cmd = ostrcat(cmd, timestamp, 1, 0);
1867        cmd = ostrcat(cmd, "_cover.jpg", 1, 0);
1868
1869        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1870        {
1871                filedebug(logfile, "#############\nLocalfile: %s/%s_cover.jpg\n#############", getconfig("mediadbpath", NULL), timestamp);
1872                cmd = ostrcat(cmd, " >> ", 1, 0);
1873                cmd = ostrcat(cmd, logfile, 1, 0);
1874                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1875        }
1876        else
1877                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1878
1879        debug(133, "cmd %s", cmd);
1880        system(cmd);
1881        free(cmd); cmd = NULL;
1882
1883        return 0;
1884}
1885
1886int mediadbffmpeg4(char* timestamp, char* tmpmeta)
1887{
1888        char* cmd = NULL;
1889
1890        if(timestamp == NULL || tmpmeta == NULL) return 1;
1891
1892        cmd = ostrcat(cmd, "ffmpeg -i ", 1, 0);
1893        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1894        cmd = ostrcat(cmd, "/", 1, 0);
1895        cmd = ostrcat(cmd, timestamp, 1, 0);
1896        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
1897        cmd = ostrcat(cmd, " > ", 1, 0);
1898        cmd = ostrcat(cmd, tmpmeta, 1, 0);
1899        cmd = ostrcat(cmd, " 2>&1", 1, 0);
1900
1901        debug(133, "cmd %s", cmd);
1902        system(cmd);
1903        free(cmd); cmd = NULL;
1904
1905        return 0;
1906}
1907
1908int mediadbffmpeg5(char* tmpjpg, char* tmpmpg, char* timestamp, char* file, char* size, off64_t filesize, char* logfile)
1909{
1910        char* cmd = NULL;
1911
1912        if(tmpjpg == NULL || tmpmpg == NULL) return 1;
1913
1914        cmd = ostrcat(cmd, "ffmpeg -y -f image2 -i ", 1, 0);
1915        cmd = ostrcat(cmd, tmpjpg, 1, 0);
1916        cmd = ostrcat(cmd, " ", 1, 0);
1917        cmd = ostrcat(cmd, tmpmpg, 1, 0);
1918
1919        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1920        {
1921                filedebug(logfile, "#############\nLocalfile: %s/%s_backdrop1.jpg size=(%s) filesize(%lld) (%s)\n#############", getconfig("mediadbpath", NULL), timestamp, size, filesize, file);
1922                cmd = ostrcat(cmd, " >> ", 1, 0);
1923                cmd = ostrcat(cmd, logfile, 1, 0);
1924                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1925        }
1926        else
1927                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1928
1929        debug(133, "cmd %s", cmd);
1930        system(cmd);
1931        free(cmd); cmd = NULL;
1932
1933        return 0;
1934}
1935
1936int mediadbcp(char* timestamp, char* poster)
1937{
1938        char* cmd = NULL;
1939
1940        if(timestamp == NULL || poster == NULL) return 1;
1941
1942        cmd = ostrcat(cmd, "cp -a ", 1, 0);
1943        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1944        cmd = ostrcat(cmd, "/", 1, 0);
1945        cmd = ostrcat(cmd, timestamp, 1, 0);
1946        cmd = ostrcat(cmd, "_cover.jpg", 1, 0);
1947        cmd = ostrcat(cmd, " ", 1, 0);
1948        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1949        cmd = ostrcat(cmd, "/", 1, 0);
1950        cmd = ostrcat(cmd, timestamp, 1, 0);
1951        cmd = ostrcat(cmd, poster, 1, 0);
1952
1953        debug(133, "cmd %s", cmd);
1954        system(cmd);
1955        free(cmd); cmd = NULL;
1956
1957        return 0;
1958}
1959
1960int mediadbcprec(char* timestamp, char* poster)
1961{
1962        char* cmd = NULL;
1963
1964        if(timestamp == NULL || poster == NULL) return 1;
1965
1966        cmd = ostrcat(cmd, "cp -a /tmp/screenshot", 1, 0);
1967        cmd = ostrcat(cmd, poster, 1, 0);
1968        cmd = ostrcat(cmd, " ", 1, 0);
1969        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1970        cmd = ostrcat(cmd, "/", 1, 0);
1971        cmd = ostrcat(cmd, timestamp, 1, 0);
1972        cmd = ostrcat(cmd, poster, 1, 0);
1973
1974        debug(133, "cmd %s", cmd);
1975        system(cmd);
1976        free(cmd); cmd = NULL;
1977
1978        cmd = ostrcat(cmd, "rm -f /tmp/screenshot", 1, 0);
1979        cmd = ostrcat(cmd, poster, 1, 0);
1980        debug(133, "cmd %s", cmd);
1981        system(cmd);
1982        free(cmd); cmd = NULL;
1983       
1984        return 0;
1985}
1986
1987void mediadbcptmdb(struct tmdb* tmdb, struct imdb* imdb)
1988{
1989        if(tmdb == NULL || imdb == NULL) return;
1990
1991        if(tmdb->imdbid != NULL) imdb->id = ostrcat(tmdb->imdbid, NULL, 0, 0);
1992        if(tmdb->title != NULL) imdb->title = ostrcat(tmdb->title, NULL, 0, 0);
1993        if(tmdb->genre != NULL) imdb->genre = ostrcat(tmdb->genre, NULL, 0, 0);
1994        if(tmdb->rating != NULL) imdb->rating = ostrcat(tmdb->rating, NULL, 0, 0);
1995        if(tmdb->votes != NULL) imdb->votes = ostrcat(tmdb->votes, NULL, 0, 0);
1996        if(tmdb->runtime != NULL) imdb->runtime = ostrcat(tmdb->runtime, NULL, 0, 0);
1997        if(tmdb->plot != NULL) imdb->plot = ostrcat(tmdb->plot, NULL, 0, 0);
1998        if(tmdb->released != NULL) imdb->released = ostrcat(tmdb->released, NULL, 0, 0);
1999        if(tmdb->postermid != NULL) imdb->poster = ostrcat(tmdb->postermid, NULL, 0, 0);
2000        if(tmdb->thumb != NULL) imdb->thumb = ostrcat(tmdb->thumb, NULL, 0, 0);
2001        if(tmdb->year != NULL) imdb->year = ostrcat(tmdb->year, NULL, 0, 0);
2002}
2003
2004void mediadbcpimdbapi(struct imdbapi* imdbapi, struct imdb* imdb)
2005{
2006        if(imdbapi == NULL || imdb == NULL) return;
2007
2008        if(imdbapi->id != NULL) imdb->id = ostrcat(imdbapi->id, NULL, 0, 0);
2009        if(imdbapi->title != NULL) imdb->title = ostrcat(imdbapi->title, NULL, 0, 0);
2010        if(imdbapi->genre != NULL) imdb->genre = ostrcat(imdbapi->genre, NULL, 0, 0);
2011        if(imdbapi->writer != NULL) imdb->writer = ostrcat(imdbapi->writer, NULL, 0, 0);
2012        if(imdbapi->director != NULL) imdb->director = ostrcat(imdbapi->director, NULL, 0, 0);
2013        if(imdbapi->actors != NULL) imdb->actors = ostrcat(imdbapi->actors, NULL, 0, 0);
2014        if(imdbapi->rating != NULL) imdb->rating = ostrcat(imdbapi->rating, NULL, 0, 0);
2015        if(imdbapi->votes != NULL) imdb->votes = ostrcat(imdbapi->votes, NULL, 0, 0);
2016        if(imdbapi->runtime != NULL) imdb->runtime = ostrcat(imdbapi->runtime, NULL, 0, 0);
2017        if(imdbapi->plot != NULL) imdb->plot = ostrcat(imdbapi->plot, NULL, 0, 0);
2018        if(imdbapi->released != NULL) imdb->released = ostrcat(imdbapi->released, NULL, 0, 0);
2019        if(imdbapi->year != NULL) imdb->year = ostrcat(imdbapi->year, NULL, 0, 0);
2020}
2021
2022void mediadbcptmdb2(struct tmdb* tmdb, struct imdb* imdb)
2023{
2024        if(tmdb == NULL || imdb == NULL) return;
2025
2026        if(imdb->id == NULL) imdb->id = ostrcat(imdb->id, tmdb->imdbid, 1, 0);
2027        if(imdb->title == NULL) imdb->title = ostrcat(imdb->title, tmdb->title, 1, 0);
2028        if(imdb->genre == NULL) imdb->genre = ostrcat(imdb->genre, tmdb->genre, 1, 0);
2029        if(imdb->rating == NULL) imdb->rating = ostrcat(imdb->rating, tmdb->rating, 1, 0);
2030        if(imdb->votes == NULL) imdb->votes = ostrcat(imdb->votes, tmdb->votes, 1, 0);
2031        if(imdb->runtime == NULL) imdb->runtime = ostrcat(imdb->runtime, tmdb->runtime, 1, 0);
2032        if(imdb->plot == NULL) imdb->plot = ostrcat(imdb->plot, tmdb->plot, 1, 0);
2033        if(imdb->released == NULL) imdb->released = ostrcat(imdb->released, tmdb->released, 1, 0);
2034        if(imdb->poster == NULL) imdb->poster = ostrcat(imdb->poster, tmdb->postermid, 1, 0);
2035        if(imdb->thumb == NULL) imdb->thumb = ostrcat(imdb->thumb, tmdb->thumb, 1, 0);
2036        if(imdb->year == NULL) imdb->year = ostrcat(imdb->year, tmdb->year, 1, 0);
2037}
2038
2039void mediadbcpimdbapi2(struct imdbapi* imdbapi, struct imdb* imdb)
2040{
2041        if(imdbapi == NULL || imdb == NULL) return;
2042
2043        if(imdb->id == NULL) imdb->id = ostrcat(imdb->id, imdbapi->id, 1, 0);
2044        if(imdb->title == NULL) imdb->title = ostrcat(imdb->title, imdbapi->title, 1, 0);
2045        if(imdb->genre == NULL) imdb->genre = ostrcat(imdb->genre, imdbapi->genre, 1, 0);
2046        if(imdb->writer == NULL) imdb->writer = ostrcat(imdb->writer, imdbapi->writer, 1, 0);
2047        if(imdb->director == NULL) imdb->director = ostrcat(imdb->director, imdbapi->director, 1, 0);
2048        if(imdb->actors == NULL) imdb->actors = ostrcat(imdb->actors, imdbapi->actors, 1, 0);
2049        if(imdb->rating == NULL) imdb->rating = ostrcat(imdb->rating, imdbapi->rating, 1, 0);
2050        if(imdb->votes == NULL) imdb->votes = ostrcat(imdb->votes, imdbapi->votes, 1, 0);
2051        if(imdb->runtime == NULL) imdb->runtime = ostrcat(imdb->runtime, imdbapi->runtime, 1, 0);
2052        if(imdb->plot == NULL) imdb->plot = ostrcat(imdb->plot, imdbapi->plot, 1, 0);
2053        if(imdb->released == NULL) imdb->released = ostrcat(imdb->released, imdbapi->released, 1, 0);
2054        if(imdb->poster == NULL) imdb->poster = ostrcat(imdb->poster, imdbapi->poster, 1, 0);
2055        if(imdb->year == NULL) imdb->year = ostrcat(imdb->year, imdbapi->year, 1, 0);
2056}
2057
2058int mediadbjpegtran(char* tmpjpg, char* timestamp)
2059{
2060        char* cmd = NULL;
2061
2062        if(tmpjpg == NULL || timestamp == NULL) return 1;
2063
2064        cmd = ostrcat(cmd, "jpegtran -outfile ", 1, 0);
2065        cmd = ostrcat(cmd, tmpjpg, 1, 0);
2066        cmd = ostrcat(cmd, " -copy none ", 1, 0);
2067        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2068        cmd = ostrcat(cmd, "/", 1, 0);
2069        cmd = ostrcat(cmd, timestamp, 1, 0);
2070        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2071
2072        debug(133, "cmd %s", cmd);
2073        system(cmd);
2074        free(cmd); cmd = NULL;
2075
2076        return 0;
2077}
2078
2079int mediadbmv(char* tmpmpg, char* timestamp)
2080{
2081        char* cmd = NULL;
2082
2083        if(tmpmpg == NULL || timestamp == NULL) return 1;
2084
2085        cmd = ostrcat(cmd, "mv -f ", 1, 0);
2086        cmd = ostrcat(cmd, tmpmpg, 1, 0);
2087        cmd = ostrcat(cmd, " ", 1, 0);
2088        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2089        cmd = ostrcat(cmd, "/", 1, 0);
2090        cmd = ostrcat(cmd, timestamp, 1, 0);
2091        cmd = ostrcat(cmd, "_backdrop1.mvi", 1, 0);
2092
2093        debug(133, "cmd %s", cmd);
2094        system(cmd);
2095        free(cmd); cmd = NULL;
2096
2097        return 0;
2098}
2099
2100// flag 0 = autoscan
2101// flag 1 = manual scan imdb
2102// flag 2 = manual scan tmdb
2103// flag 3 = manual scan imdbapi
2104void mediadbfindfilecb(char* path, char* file, int type, char* id, int flag)
2105{
2106        debug(133, "path: %s",path);
2107        debug(133, "file: %s",file);
2108        debug(133, "type: %d", type);
2109        debug(133, "id: %s",id);
2110        debug(133, "flag: %d", flag);
2111       
2112        int isrec = 0;
2113        int iscam = 0;
2114        int backdrop = 0;
2115        char* shortpath = NULL, *tmpstr = NULL, *tmpid = NULL;
2116        struct mediadb *node = NULL, *cmediadb = NULL;
2117
2118        if(id != NULL);
2119                tmpid = ostrcat(tmpid, id, 1, 0);
2120               
2121        shortpath = delmountpart(path, 0);
2122        if(shortpath == NULL) return; //no mountpart found
2123
2124        //check if entry exist
2125        node = mediadbcheckfile(file, path, shortpath);
2126
2127        int tout = getconfigint("mediadbscantimeout", NULL);
2128
2129        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)))
2130        {
2131                if(type == 0)
2132                {               
2133                        struct imdb* imdb = NULL;
2134                        struct imdbapi* imdbapi = NULL;
2135                        struct tmdb* tmdb = NULL;
2136                       
2137                        char* shortname = createshortname(file, &isrec, &iscam, 1);
2138                        char* fileinfo = createshortname(file, &isrec, &iscam, 2);
2139
2140                        char* logdir = NULL, *logfile = NULL, *tmpjpg = NULL, *tmpmpg = NULL, *tmpmeta = NULL, *timen = NULL;
2141
2142                        logdir = ostrcat(getconfig("mediadbpath", NULL), "/.mediadbdebug", 0, 0);
2143                        if(getconfigint("mediadbscandelall", NULL) == 1)
2144                                unlink(logdir);
2145                               
2146                        if(!file_exist(logdir))
2147                                mkdir(logdir, 0777);
2148                        logfile = ostrcat(logdir, "/imdb-scan.log", 0, 0);
2149
2150                        timen = ostrcat(oitoa(time(NULL)), NULL, 1, 0);
2151                       
2152                        tmpjpg = ostrcat("/tmp/backdrop.resize.", timen, 0, 0);
2153                        tmpjpg = ostrcat(tmpjpg, ".jpg", 1, 0);
2154                       
2155                        tmpmpg = ostrcat("/tmp/backdrop.resize.", timen, 0, 0);
2156                        tmpmpg = ostrcat(tmpmpg, ".mpg", 1, 0);
2157                       
2158                        tmpmeta = ostrcat("/tmp/mediadb.", timen, 0, 0);
2159                        tmpmeta = ostrcat(tmpmeta, ".meta", 1, 0);
2160                       
2161                        if(getconfigint("mediadbdebug", NULL) == 1)
2162                                filedebug(logfile, "#############\nfile: %s\nshortname: %s\nfileinfo: %s\n#############", file, shortname, fileinfo);
2163       
2164                        //got imdb infos
2165                        struct skin* imdbplugin = getplugin("IMDb");
2166                        if(imdbplugin != NULL)
2167                        {
2168                                struct imdb* (*startplugin)(struct imdb**, char*, int, int, int);
2169                                startplugin = dlsym(imdbplugin->pluginhandle, "getimdb");
2170                                if(startplugin != NULL)
2171                                {
2172                                        if((flag == 1) || (flag == 2))
2173                                        {
2174                                                tmpid = string_replace("tt", "", tmpid, 1);
2175                                                imdb = startplugin(&imdb, tmpid, 2, 0, 0); //load with imdbid with save
2176                                        }
2177                                        else
2178                                                imdb = startplugin(&imdb, shortname, 0, 1, 0);
2179                                }
2180                        }
2181
2182                        struct skin* imdbapiplugin = NULL;
2183                        struct skin* tmdbplugin = NULL;
2184
2185                        if(flag > 0 || (isrec == 0 && iscam == 0))
2186                        {
2187                                if(flag == 2 && imdb != NULL && id != NULL)
2188                                        imdb->id = ostrcat(id, NULL, 0, 0);
2189       
2190                                if(flag == 3 && imdb != NULL && id != NULL)
2191                                        imdb->id = ostrcat(id, NULL, 0, 0);
2192               
2193                                imdbapiplugin = getplugin("IMDb-API");
2194                                if(imdbplugin != NULL)
2195                                {
2196                                        struct imdbapi* (*startplugin)(struct imdbapi**, char*, int, int);
2197                                        startplugin = dlsym(imdbapiplugin->pluginhandle, "getimdbapi");
2198                                        if(startplugin != NULL)
2199                                        {
2200                                                if(imdb == NULL)
2201                                                        imdbapi = startplugin(&imdbapi, shortname, 0, 1);
2202                                                else if(imdb->id != NULL)
2203                                                        imdbapi = startplugin(&imdbapi, imdb->id, 1, 1);
2204                                        }
2205                                }
2206       
2207                                tmdbplugin = getplugin("TMDb");
2208                                if(tmdbplugin != NULL)
2209                                {
2210                                        struct tmdb* (*startplugin)(struct tmdb**, char*, int, int);
2211                                        startplugin = dlsym(tmdbplugin->pluginhandle, "gettmdb");
2212                                        if(startplugin != NULL)
2213                                        {
2214                                                if(imdb != NULL && imdb->id != NULL && flag > 0)
2215                                                        tmdb = startplugin(&tmdb, imdb->id, 1, 2);
2216                                                else if(imdbapi != NULL && imdbapi->id != NULL && flag > 0)
2217                                                        tmdb = startplugin(&tmdb, imdbapi->id, 1, 2);
2218                                                else if(imdb != NULL && imdb->id != NULL)
2219                                                        tmdb = startplugin(&tmdb, imdb->id, 1, 1);
2220                                                else if(imdbapi != NULL && imdbapi->id != NULL)
2221                                                        tmdb = startplugin(&tmdb, imdbapi->id, 1, 1);
2222                                        }
2223                                }
2224       
2225                                debugimdbnode(imdb);
2226                                if(flag == 2) mediadbcptmdb(tmdb, imdb); // manuel tmdb
2227                                debugimdbnode(imdb);
2228                                if(flag == 3) mediadbcpimdbapi(imdbapi, imdb); // manuel imdbapi
2229                                mediadbcptmdb2(tmdb, imdb);
2230                                debugimdbnode(imdb);
2231                                mediadbcpimdbapi2(imdbapi, imdb);
2232
2233                                if(tmdb != NULL && tmdb->mvi != NULL) backdrop = atoi(tmdb->mvi);
2234                        }
2235                        else if((cmpfilenameext(file, ".ts") == 0) || (cmpfilenameext(file, ".mts") == 0))
2236                        {
2237                                char* poster = NULL, *plot = NULL, *timestamp = NULL, *cmd = NULL, *tmpstr1 = NULL;
2238
2239                                timestamp = oregex(".*([0-9]{14,14}).*", file);
2240                                if(timestamp == NULL)
2241                                        timestamp = ostrcat(oitoa(time(NULL)), NULL, 1, 0);
2242                               
2243                                shortname = string_replace("   ", " - ", shortname, 1);
2244                                shortname = string_replace("  ", " - ", shortname, 1);
2245
2246                                poster = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2247                                poster = ostrcat(poster, timestamp, 1, 0);
2248                                poster = ostrcat(poster, "_poster.jpg", 1, 0);
2249
2250                                tmpstr = ostrcat(tmpstr, path, 1, 0);
2251                                tmpstr = ostrcat(tmpstr, "/", 1, 0);
2252                                tmpstr = ostrcat(tmpstr, file, 1, 0);
2253                                tmpstr1 = changefilenameext(tmpstr, ".epg");
2254                                free(tmpstr); tmpstr = NULL;
2255                                cmd = readfiletomem(tmpstr1, 1);
2256                                free(tmpstr1); tmpstr1 = NULL;
2257                                plot = ostrcat(plot, cmd, 1, 0);
2258                                free(cmd); cmd = NULL;
2259
2260                                if(file_exist("/tmp/screenshot_backdrop1.jpg"))
2261                                        mediadbcprec(timestamp, "_backdrop1.jpg");
2262                                else
2263                                        mediadbffmpeg1(file, path, timestamp, logfile);
2264
2265// bilder alle unscharf
2266/*
2267                                int channels = 0;
2268                                unsigned long width = 0, height = 0, rowbytes = 0;
2269                                unsigned char* buf = NULL;
2270
2271                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2272                                cmd = ostrcat(cmd, "/", 1, 0);
2273                                cmd = ostrcat(cmd, timestamp, 1, 0);
2274                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2275                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2276                                free(cmd), cmd = NULL;
2277                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2278                                cmd = ostrcat(cmd, timestamp, 1, 0);
2279                                cmd = ostrcat(cmd, "_thumb.jpg", 1, 0);
2280                                buf = savejpg(cmd, width, height, channels, 91, 140, 70, buf);
2281                                free(cmd); cmd = NULL;
2282                                free(buf); buf = NULL;
2283
2284                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2285                                cmd = ostrcat(cmd, "/", 1, 0);
2286                                cmd = ostrcat(cmd, timestamp, 1, 0);
2287                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2288                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2289                                free(cmd), cmd = NULL;
2290                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2291                                cmd = ostrcat(cmd, timestamp, 1, 0);
2292                                cmd = ostrcat(cmd, "_cover.jpg", 1, 0);
2293                                buf = savejpg(cmd, width, height, channels, 185, 264, 70, buf);
2294                                free(cmd); cmd = NULL;
2295                                free(buf); buf = NULL;
2296
2297                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2298                                cmd = ostrcat(cmd, "/", 1, 0);
2299                                cmd = ostrcat(cmd, timestamp, 1, 0);
2300                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2301                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2302                                free(cmd), cmd = NULL;
2303                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2304                                cmd = ostrcat(cmd, timestamp, 1, 0);
2305                                cmd = ostrcat(cmd, "_poster.jpg", 1, 0);
2306                                buf = savejpg(cmd, width, height, channels, 400, 450, 70, buf);
2307                                free(cmd); cmd = NULL;
2308                                free(buf); buf = NULL;
2309
2310                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2311                                cmd = ostrcat(cmd, "/", 1, 0);
2312                                cmd = ostrcat(cmd, timestamp, 1, 0);
2313                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2314                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2315                                free(cmd), cmd = NULL;
2316                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2317                                cmd = ostrcat(cmd, timestamp, 1, 0);
2318                                cmd = ostrcat(cmd, "_postermid.jpg", 1, 0);
2319                                buf = savejpg(cmd, width, height, channels, 400, 450, 70, buf);
2320                                free(cmd); cmd = NULL;
2321                                free(buf); buf = NULL;
2322*/
2323
2324                                if(file_exist("/tmp/screenshot_thumb.jpg"))
2325                                        mediadbcprec(timestamp, "_thumb.jpg");
2326                                else
2327                                        mediadbffmpeg2(file, path, timestamp, logfile);
2328
2329                                if(file_exist("/tmp/screenshot_cover.jpg"))
2330                                        mediadbcprec(timestamp, "_cover.jpg");
2331                                else
2332                                        mediadbffmpeg3(file, path, timestamp, logfile);
2333
2334                                mediadbcp(timestamp, "_poster.jpg");
2335                                mediadbcp(timestamp, "_postermid.jpg");
2336
2337                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2338                                cmd = ostrcat(cmd, "/", 1, 0);
2339                                cmd = ostrcat(cmd, timestamp, 1, 0);
2340                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2341                               
2342                                off64_t filesize = getfilesize(cmd);
2343                                debug(133, "filesize %lld", filesize);
2344                                free(cmd); cmd = NULL;
2345                                                               
2346                                if(filesize < 1500000)
2347                                {
2348                                        mediadbffmpeg4(timestamp, tmpmeta);
2349                                               
2350                                        cmd = ostrcat(cmd, "cat ", 1, 0);
2351                                        cmd = ostrcat(cmd, tmpmeta, 1, 0);
2352                                        cmd = ostrcat(cmd, " | grep Stream | awk '{print $6}' | cut -d'x' -f1", 1, 0);
2353                                        char* size = string_newline(command(cmd));
2354                                        free(cmd); cmd= NULL;
2355                                        debug(133, "size %s", size);
2356                                        if(size != NULL)
2357                                        {
2358                                                debug(133, "size %d", atoi(size));
2359                                                int picsize = atoi(size);
2360       
2361                                                if(picsize < 2000)
2362                                                {
2363                                                        debug(133, "size ok %d", picsize);
2364                                                        mediadbjpegtran(tmpjpg, timestamp);
2365                                               
2366                                                        if(file_exist(tmpjpg))
2367                                                        {
2368                                                                mediadbffmpeg5(tmpjpg, tmpmpg, timestamp, file, size, filesize, logfile);
2369
2370                                                                if(file_exist(tmpmpg))
2371                                                                {
2372                                                                        mediadbmv(tmpmpg, timestamp);
2373                                                                        backdrop = 1;
2374                                                                        writesysint("/proc/sys/vm/drop_caches", 3, 0);
2375
2376                                                                        debug(777, "imdb id %s", timestamp);
2377                                                                        if(cmediadb == NULL) cmediadb = createmediadb(node, timestamp, type, shortname, NULL, NULL, NULL, NULL, NULL, NULL, NULL, plot, poster, NULL, NULL, shortpath, file, shortname, fileinfo, 0, backdrop);
2378                                                                }       
2379                                                        }
2380                                                }
2381                                                else
2382                                                {
2383                                                        debug(133, "ERROR Localfile size to big skipped %d", picsize);
2384       
2385                                                        if(getconfigint("mediadbdebug", NULL) == 1)
2386                                                                filedebug(logfile, "#############\nERROR Localfile size to big skipped: %s/%s_backdrop1.jpg size=(%s) filesize(%lld) (%s)\n#############", getconfig("mediadbpath", NULL), timestamp, size, filesize, file);
2387                                                }
2388                                        }
2389                                        else
2390                                        {
2391                                                debug(133, "ERROR Localfile size is NULL skipped %s", size);
2392       
2393                                                if(getconfigint("mediadbdebug", NULL) == 1)
2394                                                        filedebug(logfile, "#############\nERROR Localfile size is NULL skipped: %s/%s_backdrop1.jpg size=(%s) filesize(%lld) (%s)\n#############", getconfig("mediadbpath", NULL), timestamp, size, filesize, file);
2395                                        }
2396                                        free(size); size = NULL;
2397                                        unlink(tmpmeta);
2398                                        unlink(tmpjpg);
2399                                        unlink(tmpmpg);
2400                                       
2401                                        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2402                                        cmd = ostrcat(cmd, "/", 1, 0); 
2403                                        cmd = ostrcat(cmd, timestamp, 1, 0);
2404                                        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2405                                        unlink(cmd);
2406                                }
2407                                else
2408                                {
2409                                        debug(133, "ERROR Localfile filesize to BIG skipped %lld", filesize);
2410               
2411                                        if(getconfigint("mediadbdebug", NULL) == 1)
2412                                                filedebug(logfile, "#############\nERROR Localfile filesize to BIG skipped: %s/%s_backdrop1.jpg filesize(%lld) (%s)\n#############", getconfig("mediadbpath", NULL), timestamp, filesize, file);
2413                                }
2414                                free(cmd); cmd = NULL;
2415                                free(timestamp); timestamp = NULL;
2416                                free(poster); poster = NULL;
2417                                free(plot); plot = NULL;
2418                        }
2419               
2420                        free(logdir); logdir = NULL;
2421                        free(logfile); logfile = NULL;
2422                        free(timen); timen = NULL;
2423                        free(tmpjpg); tmpjpg = NULL;
2424                        free(tmpmpg); tmpmpg = NULL;
2425                        free(tmpmeta); tmpmeta = NULL;
2426
2427                        debugimdbnode(imdb);
2428                       
2429                        if(cmediadb == NULL)
2430                        {
2431                                debug(777, "add video: %s/%s", shortpath, file);
2432                                if(imdb != NULL)
2433                                {
2434                                        debug(777, "imdb id %s", imdb->id);
2435                                        cmediadb = 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, shortname, fileinfo, 0, backdrop);
2436                                        if(tmpid != NULL)
2437                                        {
2438                                                char* tmpstr = NULL;
2439                                                tmpstr = ostrcat(tmpstr, _("file"), 1, 0);
2440                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2441                                                tmpstr = ostrcat(tmpstr, file, 1, 0);                                                   
2442                                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
2443                                                tmpstr = ostrcat(tmpstr, _("path"), 1, 0);
2444                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2445                                                tmpstr = ostrcat(tmpstr, path, 1, 0);                                                   
2446                                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
2447                                                tmpstr = ostrcat(tmpstr, _("title"), 1, 0);
2448                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2449                                                tmpstr = ostrcat(tmpstr, imdb->title, 1, 0);                                                   
2450                                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
2451                                                tmpstr = ostrcat(tmpstr, _("imdbid"), 1, 0);
2452                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2453                                                tmpstr = ostrcat(tmpstr, id, 1, 0);                                                                                                                                                                                                             
2454                                                textbox(_("Add iMDB manuel"), tmpstr, _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 500, 10, 0);
2455                                                free(tmpstr); tmpstr = NULL;
2456                                        }
2457                                }
2458                                else
2459                                        cmediadb = createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, shortname, fileinfo, 0, 0);
2460                        }
2461
2462                        debug(777, "shortname: %s", shortname);
2463                        debug(133, "shortname: %s", shortname);
2464                        free(shortname); shortname = NULL;
2465
2466                        debug(777, "fileinfo: %s", fileinfo);
2467                        debug(133, "fileinfo: %s", fileinfo);
2468                        free(fileinfo); fileinfo = NULL;
2469                                               
2470                        if(imdbplugin != NULL)
2471                        {
2472                                void (*startplugin)(struct imdb**, int flag);
2473                                startplugin = dlsym(imdbplugin->pluginhandle, "freeimdb");
2474                                if(startplugin != NULL)
2475                                        startplugin(&imdb, 0);
2476                        }
2477                        imdb = NULL;
2478                       
2479                        if(imdbapiplugin != NULL)
2480                        {
2481                                void (*startplugin)(struct imdbapi**, int);
2482                                startplugin = dlsym(imdbapiplugin->pluginhandle, "freeimdbapi");
2483                                if(startplugin != NULL)
2484                                        startplugin(&imdbapi, 0);
2485                        }
2486                        imdbapi = NULL;
2487
2488                        if(tmdbplugin != NULL)
2489                        {
2490                                void (*startplugin)(struct tmdb**, int);
2491                                startplugin = dlsym(tmdbplugin->pluginhandle, "freetmdb");
2492                                if(startplugin != NULL)
2493                                        startplugin(&tmdb, 0);
2494                        }
2495                        tmdb = NULL;
2496                }
2497                else if(type == 1)
2498                {
2499                        struct id3tag* id3tag = NULL;
2500
2501                        debug(777, "add audio: %s/%s", shortpath, file);
2502
2503                        // create filelist info
2504                        free(tmpstr), tmpstr = NULL;
2505                        tmpstr = ostrcat(tmpstr, file, 1, 0);
2506                        string_tolower(tmpstr);
2507                        char* fileinfo = NULL;
2508                        fileinfo = ostrcat(fileinfo, getfilenameext(tmpstr), 1, 1);
2509                        free(tmpstr), tmpstr = NULL;
2510
2511                        char* tmpfile = ostrcat(path, "/", 0, 0);
2512                        tmpfile = ostrcat(tmpfile, file, 1, 0);
2513
2514                        int hash = gethash(tmpfile);
2515                        char* tmphash = olutoa(hash);
2516
2517                        id3tag = getid3(tmpfile, tmphash, 1);
2518
2519                        if(id3tag != NULL)
2520                        {
2521                                if(id3tag->poster != NULL)
2522                                        cmediadb = createmediadb(node, tmphash, type, id3tag->title, id3tag->year, NULL, NULL, id3tag->genretext, NULL, NULL, id3tag->artist, id3tag->album, tmphash, NULL, NULL, shortpath, file, NULL, fileinfo, 0, 0);
2523                                else
2524                                        cmediadb = createmediadb(node, tmphash, type, id3tag->title, id3tag->year, NULL, NULL, id3tag->genretext, NULL, NULL, id3tag->artist, id3tag->album, NULL, NULL, NULL, shortpath, file, NULL, fileinfo, 0, 0);
2525                        }
2526                        else
2527                                cmediadb = createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, NULL, NULL, 0, 0);
2528
2529                        free(tmpfile); tmpfile = NULL;
2530                        free(tmphash); tmphash = NULL;
2531                        free(fileinfo); fileinfo = NULL;
2532                        freeid3(id3tag); id3tag = NULL;
2533                }
2534                else if(type == 2)
2535                {
2536                        char* thumbfile = NULL;
2537
2538                        debug(777, "add pic: %s/%s", shortpath, file);
2539                        if(status.thumbthread != NULL)
2540                        {
2541                                //check if thumb exists
2542                                thumbfile = checkthumb(path, file);
2543                                if(thumbfile == NULL)
2544                                        addqueue(101, (void*)path, strlen(path) + 1, (void*)file, strlen(file) + 1, 0, NULL);
2545                        }
2546                        free(thumbfile); thumbfile = NULL;
2547
2548                        cmediadb = createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, NULL, NULL, 0, 0);
2549                }
2550        }
2551        free(shortpath); shortpath = NULL;
2552
2553        //first time save full mediadb, rest save only akt entry
2554        if(status.mediadbsavetime == 0)
2555        {
2556                writemediadb(getconfig("mediadbfile", NULL), NULL);
2557                status.mediadbsavetime = 1;
2558        }
2559        else if(cmediadb != NULL)
2560                writemediadb(getconfig("mediadbfile", NULL), cmediadb);
2561}
2562
2563int findfiles(char* dirname, int type, int onlydir, int onlycount, int first)
2564{
2565        debug(777, "dir=%s type=%d onlydir=%d, onlycount=%d", dirname, type, onlydir, onlycount);
2566        DIR *d;
2567        char* tmpstr = NULL;
2568
2569        //Open the directory specified by dirname
2570        d = opendir(dirname);
2571
2572        //Check it was opened
2573        if(! d)
2574        {
2575                perr("Cannot open directory %s", dirname);
2576                return -1;
2577        }
2578
2579        int count = 0;
2580
2581        while(1)
2582        {
2583                struct dirent* entry;
2584                int path_length;
2585                char path[PATH_MAX];
2586
2587                snprintf(path, PATH_MAX, "%s", dirname);
2588                //Readdir gets subsequent entries from d
2589                entry = readdir(d);
2590
2591                if(!entry) //There are no more entries in this directory, so break out of the while loop
2592                        break;
2593
2594                //for nfs mounts if file type is unknown use stat
2595                if(entry->d_type == DT_UNKNOWN)
2596                {
2597                        tmpstr = ostrcat(dirname, "/", 0, 0);
2598                        tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0);
2599                        entry->d_type = getfiletype(tmpstr);
2600                        free(tmpstr); tmpstr = NULL;
2601                }
2602
2603                //check if link is a dir
2604                if(first == 1 && entry->d_type == DT_LNK)
2605                {
2606                        tmpstr = createpath(path, entry->d_name);
2607                        if(isdir(tmpstr) == 1)
2608                                entry->d_type = DT_DIR;
2609
2610                        free(tmpstr); tmpstr = NULL;
2611                }
2612
2613                //See if entry is a subdirectory of d
2614                if(entry->d_type == DT_DIR)
2615                {
2616                        //Check that the directory is not d or d's parent
2617                        if(entry->d_name != NULL && entry->d_name[0] != '.' && entry->d_name[0] != '$')
2618                        {
2619                                path_length = snprintf(path, PATH_MAX, "%s/%s", dirname, entry->d_name);
2620                                if(path_length >= PATH_MAX)
2621                                {
2622                                        err("path length has got too long");
2623                                        if(d) closedir(d);
2624                                        return -1;
2625                                }
2626                                //Recursively call findfiles with the new path
2627                                if(onlydir == 0)
2628                                        findfiles(path, type, onlydir, onlycount, 0);
2629                        }
2630                }
2631                else //File
2632                {
2633                        //TODO: add extensions
2634                       
2635                        if(file_exist("/mnt/swapextensions/etc/.codecpack") || file_exist("/var/swap/etc/.codecpack") || file_exist("/var/etc/.codecpack"))
2636                        {
2637                                if(!filelistflt(".avi .dat .divx .flv .mkv .m4v .mp4 .mov .mpg .mpeg .mts .m2ts .trp .ts .vdr .vob .wmv .rm", entry->d_name)) //video
2638                                {                               
2639                                        if(type == 0 || type == 100 || type == 90 || type == 91)
2640                                        {                                       
2641                                                if(onlycount == 0)
2642                                                        mediadbfindfilecb(path, entry->d_name, 0, NULL, 0);
2643                                                else
2644                                                        count += 1;
2645                                        }
2646                                }
2647                                else if(!filelistflt(".mp3 .flac .ogg .wma .ra .wav", entry->d_name)) //audio
2648                                {
2649                                        if(type == 1 || type == 100 || type == 90 || type == 92)
2650                                        {
2651                                                if(onlycount == 0)
2652                                                        mediadbfindfilecb(path, entry->d_name, 1, NULL, 0);
2653                                                else
2654                                                        count += 1;
2655                                        }
2656                                }
2657                                else if(!filelistflt(".jpg .png", entry->d_name)) //picture
2658                                {
2659                                        if(type == 2 || type == 100 || type == 91 || type == 92)
2660                                        {
2661                                                if(onlycount == 0)
2662                                                        mediadbfindfilecb(path, entry->d_name, 2, NULL, 0);
2663                                                else
2664                                                        count += 1;
2665                                        }
2666                                }
2667                        }
2668                        else
2669                        {
2670                                if(!filelistflt(".avi .mkv .mpg .mpeg .ts", entry->d_name)) //video
2671                                {                               
2672                                        if(type == 0 || type == 100 || type == 90 || type == 91)
2673                                        {
2674                                                if(onlycount == 0)
2675                                                        mediadbfindfilecb(path, entry->d_name, 0, NULL, 0);
2676                                                else
2677                                                        count += 1;
2678                                        }
2679                                }
2680                                else if(!filelistflt(".mp3 .flac .ogg", entry->d_name)) //audio
2681                                {
2682                                        if(type == 1 || type == 100 || type == 90 || type == 92)
2683                                        {
2684                                                if(onlycount == 0)
2685                                                        mediadbfindfilecb(path, entry->d_name, 1, NULL, 0);
2686                                                else
2687                                                        count += 1;
2688                                        }
2689                                }
2690                                else if(!filelistflt(".jpg .png", entry->d_name)) //picture
2691                                {
2692                                        if(type == 2 || type == 100 || type == 91 || type == 92)
2693                                        {
2694                                                if(onlycount == 0)
2695                                                        mediadbfindfilecb(path, entry->d_name, 2, NULL, 0);
2696                                                else
2697                                                        count += 1;
2698                                        }
2699                                }
2700                        }
2701                }
2702        }
2703
2704        //After going through all the entries, close the directory
2705        if(d && closedir(d))
2706        {
2707                perr("Could not close %s", dirname);
2708                return -1;
2709        }
2710
2711        if(onlycount == 1)
2712                return count;
2713
2714        return 0;
2715}
2716
2717//type 0=video, 1=audio, 2=pic, 90=video/audio, 91=video/pic, 92=audio/pic, 100=all
2718//flag: 0 = scan recursive
2719//flag: 1 = not scan recursive
2720void mediadbscan(char* path, int type, int flag)
2721{
2722        int count = 0;
2723        if(flag == 1) type = type | 0x80000000;
2724
2725        //param1 (path) is freed in thread
2726        addtimer(&mediadbscanthread, START, 1000, 1, (void*)ostrcat(path, NULL, 0, 0), (void*)type, NULL);
2727
2728        //block a little
2729        while(status.mediadbthread != NULL && count < 20)
2730        {
2731                usleep(100000);
2732                count++;
2733        }
2734}
2735
2736#endif
Note: See TracBrowser for help on using the repository browser.