source: titan/titan/mediadb.h @ 43326

Last change on this file since 43326 was 40424, checked in by gost, 7 years ago

fix some warnings

File size: 73.5 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 = getrandomnum(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, *tmpstr1 = 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
1498printf("###### strip () strings start ############################################\n");
1499
1500// strip () strings
1501// for (channel)-movie-(..).ts name
1502        tmpstr1 = string_resub("(", ")", shortname, 0);
1503        if(tmpstr1 != NULL)
1504        {
1505printf("found recnew filename strip (channel)-movie-(..).ts name from: %s\n", tmpstr);
1506                tmpstr1 = ostrcat("(", tmpstr1, 0, 1);
1507                tmpstr1 = ostrcat(tmpstr1, ")", 1, 0);
1508printf("shortname: %s\n", shortname);
1509printf("tmpstr1: %s\n", tmpstr1);
1510                shortname = string_replace(tmpstr1, "", shortname, 1);
1511printf("shortname stripped: %s\n", shortname);
1512printf("--------------------------------------------------\n");
1513                if(fileinfo != NULL)
1514                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1515                fileinfo = ostrcat(fileinfo, "recnew", 1, 0);
1516                *isrec = 0;
1517                *iscam = 0;
1518        }
1519        free(tmpstr1); tmpstr1 = NULL;
1520
1521// for movie-(channel-...).ts name
1522        tmpstr1 = string_resub("(", ")", shortname, 0);
1523        if(tmpstr1 != NULL)
1524        {
1525printf("found recnew filename strip movie-(channel-...).ts name from: %s\n", tmpstr);
1526                tmpstr1 = ostrcat("(", tmpstr1, 0, 1);
1527                tmpstr1 = ostrcat(tmpstr1, ")", 1, 0);
1528printf("shortname: %s\n", shortname);
1529printf("tmpstr1: %s\n", tmpstr1);
1530                shortname = string_replace(tmpstr1, "", shortname, 1);
1531printf("shortname stripped: %s\n", shortname);
1532printf("--------------------------------------------------\n");
1533                if(fileinfo != NULL)
1534                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1535                fileinfo = ostrcat(fileinfo, "recnew", 1, 0);
1536                *isrec = 0;
1537                *iscam = 0;
1538        }
1539        free(tmpstr1); tmpstr1 = NULL;
1540// end
1541printf("###### strip () strings end ############################################\n");
1542
1543        tmpstr1 = oregex(".*([0-9]{14,14}).*", tmpstr);
1544        if(tmpstr1 != NULL)
1545        {
1546                shortname = string_replace(tmpstr1, "", shortname, 1);
1547                if(fileinfo != NULL)
1548                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1549                fileinfo = ostrcat(fileinfo, "rec", 1, 0);
1550                *isrec = 1;
1551        }                       
1552        free(tmpstr1); tmpstr1 = NULL;
1553
1554        tmpstr1 = oregex(".*([0-9]{8,8}.*[0-9]{4,4}).*", tmpstr);
1555        if(tmpstr1 != NULL && *isrec == 0)
1556        {
1557                shortname = string_replace(tmpstr1, "", shortname, 1);
1558                if(fileinfo != NULL)
1559                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1560                fileinfo = ostrcat(fileinfo, "recold", 1, 0);
1561                *isrec = 1;
1562        }
1563        free(tmpstr1); tmpstr1 = NULL;
1564
1565        tmpstr1 = oregex(".*([0-9]{5,5}).*", tmpstr);
1566        if(tmpstr1 != NULL && *isrec == 0)
1567        {
1568                *iscam = 1;
1569                if(fileinfo != NULL)
1570                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1571                fileinfo = ostrcat(fileinfo, "cam", 1, 0);
1572        }
1573        free(tmpstr1), tmpstr1 = NULL;
1574
1575        if(*isrec == 0 && *iscam == 0)
1576        {
1577                string_tolower(shortname);
1578                shortname = string_shortname(shortname, 2);
1579                string_removechar(shortname);
1580                strstrip(shortname);
1581        }
1582        else
1583        {
1584                char* cut = ostrcat(".", getfilenameext(tmpstr), 0, 1);
1585                cut = ostrcat(cut, "\0", 1, 0);
1586                shortname = string_replace(cut, "\0", shortname, 1);
1587                free(cut); cut = NULL;
1588
1589                string_removechar(shortname);
1590                strstrip(shortname);
1591        }
1592
1593        string_tolower(tmpstr);
1594
1595        if((ostrstr(tmpstr, ".special.extended.edition.") != NULL) || (ostrstr(tmpstr, ".sse.") != NULL))
1596        {
1597                if(fileinfo != NULL)
1598                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1599                fileinfo = ostrcat(fileinfo, "SEE-Version", 1, 0);
1600                shortname = string_replace(".special.extended.edition.", "", shortname, 1);
1601        }
1602        if((ostrstr(tmpstr, ".extended.version.") != NULL) || (ostrstr(tmpstr, ".extended.") != NULL) || (ostrstr(tmpstr, ".ed.") != NULL))
1603        {
1604                if(fileinfo != NULL)
1605                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1606                fileinfo = ostrcat(fileinfo, "ED-Version", 1, 0);
1607                shortname = string_replace(".extended.version.", "", shortname, 1);
1608        }
1609        if((ostrstr(tmpstr, ".uncut.") != NULL) || (ostrstr(tmpstr, ".uc.") != NULL))
1610        {
1611                if(fileinfo != NULL)
1612                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1613                fileinfo = ostrcat(fileinfo, "UC-Version", 1, 0);
1614        }
1615        if((ostrstr(tmpstr, ".unrated.") != NULL) || (ostrstr(tmpstr, ".ur.") != NULL))
1616        {
1617                if(fileinfo != NULL)
1618                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1619                fileinfo = ostrcat(fileinfo, "UR-Version", 1, 0);
1620        }
1621        if((ostrstr(tmpstr, ".telesync.") != NULL) || (ostrstr(tmpstr, ".ts.") != NULL))
1622        {
1623                if(fileinfo != NULL)
1624                        fileinfo = ostrcat(fileinfo, " ", 1, 0);                       
1625                fileinfo = ostrcat(fileinfo, "telesync", 1, 0);
1626        }
1627        if((ostrstr(tmpstr, ".telecine.") != NULL) || (ostrstr(tmpstr, ".tc.") != NULL))
1628        {
1629                if(fileinfo != NULL)
1630                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1631                fileinfo = ostrcat(fileinfo, "telecine", 1, 0);
1632        }
1633        if((ostrstr(tmpstr, ".dts.") != NULL) || (ostrstr(tmpstr, ".dtshd.") != NULL))
1634        {
1635                if(fileinfo != NULL)
1636                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1637                fileinfo = ostrcat(fileinfo, "dts", 1, 0);
1638        }
1639        if((ostrstr(tmpstr, ".ac3.") != NULL) || (ostrstr(tmpstr, ".ac3d.") != NULL) || (ostrstr(tmpstr, ".ac3hd.") != NULL))
1640        {
1641                if(fileinfo != NULL)
1642                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1643                fileinfo = ostrcat(fileinfo, "ac3", 1, 0);
1644        }
1645        if(ostrstr(tmpstr, ".r5.") != NULL)
1646        {
1647                if(fileinfo != NULL)
1648                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1649                fileinfo = ostrcat(fileinfo, "r5", 1, 0);
1650        }
1651        if(ostrstr(tmpstr, ".r3.") != NULL)
1652        {
1653                if(fileinfo != NULL)
1654                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1655                fileinfo = ostrcat(fileinfo, "r3", 1, 0);
1656        }
1657        if(ostrstr(tmpstr, ".r1.") != NULL)
1658        {
1659                if(fileinfo != NULL)
1660                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1661                fileinfo = ostrcat(fileinfo, "r1", 1, 0);
1662        }                       
1663        if(ostrstr(tmpstr, ".sample.") != NULL)
1664        {
1665                if(fileinfo != NULL)
1666                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1667                fileinfo = ostrcat(fileinfo, "sample", 1, 0);
1668        }
1669
1670        tmpstr1 = oregex(".*([0-9]{4,4}).*", tmpstr);
1671        if(tmpstr1 != NULL && *isrec == 0 && *iscam == 0)
1672        {
1673                if(fileinfo != NULL)
1674                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1675                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1676        }
1677        free(tmpstr1); tmpstr1 = NULL;
1678       
1679        tmpstr1 = oregex(".*(cd[0-9]{1,3}).*", tmpstr);
1680        if(tmpstr1 != NULL)
1681        {
1682                if(fileinfo != NULL)
1683                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1684                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1685                shortname = string_replace(tmpstr1, "", shortname, 1);
1686        }
1687        free(tmpstr1); tmpstr1 = NULL;
1688
1689        tmpstr1 = oregex(".*(dvd[0-9]{1,2}).*", tmpstr);
1690        if(tmpstr1 != NULL)
1691        {
1692                if(fileinfo != NULL)
1693                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1694                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1695                shortname = string_replace(tmpstr1, "", shortname, 1);
1696        }
1697        free(tmpstr1); tmpstr1 = NULL;
1698
1699        tmpstr1 = oregex(".*(s[0-9]{1,2}e[0-9]{1,2}e[0-9]{1,2}).*", tmpstr);
1700        if(tmpstr1 != NULL)
1701        {
1702                if(fileinfo != NULL)
1703                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1704                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1705                shortname = string_replace(tmpstr1, "", shortname, 1);
1706        }
1707        free(tmpstr1); tmpstr1 = NULL;
1708
1709        tmpstr1 = oregex(".*(s[0-9]{1,2}e[0-9]{1,2}).*", tmpstr);
1710        if(tmpstr1 != NULL)
1711        {
1712                if(fileinfo != NULL)
1713                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1714                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1715                shortname = string_replace(tmpstr1, "", shortname, 1);
1716        }
1717        free(tmpstr1); tmpstr1 = NULL;
1718
1719        tmpstr1 = oregex(".*(disc[0-9]{1,2}).*", tmpstr);
1720        if(tmpstr1 != NULL)
1721        {
1722                if(fileinfo != NULL)
1723                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1724                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1725                shortname = string_replace(tmpstr1, "", shortname, 1);
1726        }
1727        free(tmpstr1); tmpstr1 = NULL;
1728
1729        tmpstr1 = oregex(".*(season[0-9]{1,2}).*", tmpstr);
1730        if(tmpstr1 != NULL)
1731        {
1732                if(fileinfo != NULL)
1733                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1734                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1735                shortname = string_replace(tmpstr1, "", shortname, 1);
1736        }
1737        free(tmpstr1); tmpstr1 = NULL;
1738
1739        tmpstr1 = oregex(".*(episode[0-9]{1,2}).*", tmpstr);
1740        if(tmpstr1 != NULL)
1741        {
1742                if(fileinfo != NULL)
1743                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1744                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1745                shortname = string_replace(tmpstr1, "", shortname, 1);
1746        }
1747        free(tmpstr1); tmpstr1 = NULL;
1748
1749        tmpstr1 = oregex(".*(staffel[0-9]{1,2}).*", tmpstr);
1750        if(tmpstr1 != NULL)
1751        {
1752                if(fileinfo != NULL)
1753                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1754                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1755                shortname = string_replace(tmpstr1, "", shortname, 1);
1756        }
1757        free(tmpstr1); tmpstr1 = NULL;
1758
1759        tmpstr1 = oregex(".*(part[0-9]{1,2}).*", tmpstr);
1760        if(tmpstr1 != NULL)
1761        {
1762                if(fileinfo != NULL)
1763                        fileinfo = ostrcat(fileinfo, " ", 1, 0);
1764                fileinfo = ostrcat(fileinfo, tmpstr1, 1, 0);
1765                shortname = string_replace(tmpstr1, "", shortname, 1);
1766        }
1767        free(tmpstr1); tmpstr1 = NULL;
1768
1769        strstrip(shortname);
1770
1771        if(fileinfo != NULL)
1772                fileinfo = ostrcat(fileinfo, " ", 1, 0);
1773        fileinfo = ostrcat(fileinfo, getfilenameext(tmpstr), 1, 1);
1774        free(tmpstr); tmpstr = NULL;
1775
1776        if(flag == 1)
1777        {
1778                free(fileinfo);
1779                return shortname;
1780        }
1781        else if(flag == 2)
1782        {
1783                free(shortname);
1784                return fileinfo;
1785        }
1786
1787        free(fileinfo);
1788        free(shortname);
1789        return NULL;
1790}
1791
1792struct mediadb* mediadbcheckfile(char* file, char* path, char* shortpath)
1793{
1794        char* tmpstr = NULL;
1795        struct mediadb *node = NULL;
1796
1797        if(file == NULL || path == NULL || shortpath == NULL) return NULL;
1798        m_lock(&status.mediadbmutex, 17);
1799
1800        //check if entry exist
1801        node = mediadb;
1802        while(node != NULL)
1803        {
1804                if(ostrcmp(node->file, file) == 0)
1805                {
1806                        //check directory
1807                        if(ostrcmp(shortpath, node->path) == 0) //same file
1808                                break;
1809
1810                        //check file size
1811                        tmpstr = ostrcat(path, "/", 0, 0);
1812                        tmpstr = ostrcat(tmpstr, file, 1, 0);
1813                        off64_t s1 = getfilesize(tmpstr);
1814                        free(tmpstr); tmpstr = NULL;
1815
1816                        tmpstr = ostrcat(node->path, "/", 0, 0);
1817                        tmpstr = ostrcat(tmpstr, node->file, 1, 0);
1818                        tmpstr = addmountpart(tmpstr, 1);
1819                        off64_t s2 = getfilesize(tmpstr);
1820                        free(tmpstr); tmpstr = NULL;
1821
1822                        if(s1 == s2) //seems the same file
1823                                break;
1824                }
1825                node = node->next;
1826        }
1827        m_unlock(&status.mediadbmutex, 17);
1828
1829        return node;
1830}
1831
1832int mediadbffmpeg1(char* file, char* path, char* timestamp, char* logfile)
1833{
1834        char* cmd = NULL;
1835
1836        if(file == NULL || path == NULL || timestamp == NULL) return 1;
1837
1838        cmd = ostrcat(cmd, "ffmpeg -i \"", 1, 0);
1839        cmd = ostrcat(cmd, path, 1, 0);
1840        cmd = ostrcat(cmd, "/", 1, 0);
1841        cmd = ostrcat(cmd, file, 1, 0);
1842//cmd = ostrcat(cmd, "\" -vframes 1 -s 1920x1080 ", 1, 0);
1843        cmd = ostrcat(cmd, "\" -vframes 1 -s 1280x720 ", 1, 0);
1844        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1845        cmd = ostrcat(cmd, "/", 1, 0);
1846        cmd = ostrcat(cmd, timestamp, 1, 0);
1847        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
1848
1849        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1850        {
1851                filedebug(logfile, "#############\nLocalfile: %s/%s_backdrop1.jpg\n#############", getconfig("mediadbpath", NULL), timestamp);
1852                cmd = ostrcat(cmd, " >> ", 1, 0);
1853                cmd = ostrcat(cmd, logfile, 1, 0);
1854                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1855        }
1856        else
1857                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1858
1859        debug(133, "cmd %s", cmd);
1860        system(cmd);
1861        free(cmd); cmd = NULL;
1862
1863        return 0;
1864}
1865
1866int mediadbffmpeg2(char* file, char* path, char* timestamp, char* logfile)
1867{
1868        char* cmd = NULL;
1869
1870        if(file == NULL || path == NULL || timestamp == NULL) return 1;
1871
1872        cmd = ostrcat(cmd, "ffmpeg -i \"", 1, 0);
1873        cmd = ostrcat(cmd, path, 1, 0);
1874        cmd = ostrcat(cmd, "/", 1, 0);
1875        cmd = ostrcat(cmd, file, 1, 0);
1876        cmd = ostrcat(cmd, "\" -vframes 1 -s 160x120 ", 1, 0);
1877        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1878        cmd = ostrcat(cmd, "/", 1, 0);
1879        cmd = ostrcat(cmd, timestamp, 1, 0);
1880        cmd = ostrcat(cmd, "_thumb.jpg", 1, 0);
1881
1882        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1883        {
1884                filedebug(logfile, "#############\nLocalfile: %s/%s_thumb.jpg\n#############", getconfig("mediadbpath", NULL), timestamp);
1885                cmd = ostrcat(cmd, " >> ", 1, 0);
1886                cmd = ostrcat(cmd, logfile, 1, 0);
1887                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1888        }
1889        else
1890                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1891
1892        debug(133, "cmd %s", cmd);
1893        system(cmd);
1894        free(cmd); cmd = NULL;
1895
1896        return 0;
1897}
1898
1899int mediadbffmpeg3(char* file, char* path, char* timestamp, char* logfile)
1900{
1901        char* cmd = NULL;
1902
1903        if(file == NULL || path == NULL || timestamp == NULL) return 1;
1904
1905        cmd = ostrcat(cmd, "ffmpeg -i \"", 1, 0);
1906        cmd = ostrcat(cmd, path, 1, 0);
1907        cmd = ostrcat(cmd, "/", 1, 0);
1908        cmd = ostrcat(cmd, file, 1, 0);
1909        cmd = ostrcat(cmd, "\" -vframes 1 -s 500x400 ", 1, 0);
1910        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1911        cmd = ostrcat(cmd, "/", 1, 0);
1912        cmd = ostrcat(cmd, timestamp, 1, 0);
1913        cmd = ostrcat(cmd, "_cover.jpg", 1, 0);
1914
1915        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1916        {
1917                filedebug(logfile, "#############\nLocalfile: %s/%s_cover.jpg\n#############", getconfig("mediadbpath", NULL), timestamp);
1918                cmd = ostrcat(cmd, " >> ", 1, 0);
1919                cmd = ostrcat(cmd, logfile, 1, 0);
1920                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1921        }
1922        else
1923                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1924
1925        debug(133, "cmd %s", cmd);
1926        system(cmd);
1927        free(cmd); cmd = NULL;
1928
1929        return 0;
1930}
1931
1932int mediadbffmpeg4(char* timestamp, char* tmpmeta)
1933{
1934        char* cmd = NULL;
1935
1936        if(timestamp == NULL || tmpmeta == NULL) return 1;
1937
1938        cmd = ostrcat(cmd, "ffmpeg -i ", 1, 0);
1939        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1940        cmd = ostrcat(cmd, "/", 1, 0);
1941        cmd = ostrcat(cmd, timestamp, 1, 0);
1942        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
1943        cmd = ostrcat(cmd, " > ", 1, 0);
1944        cmd = ostrcat(cmd, tmpmeta, 1, 0);
1945        cmd = ostrcat(cmd, " 2>&1", 1, 0);
1946
1947        debug(133, "cmd %s", cmd);
1948        system(cmd);
1949        free(cmd); cmd = NULL;
1950
1951        return 0;
1952}
1953
1954int mediadbffmpeg5(char* tmpjpg, char* tmpmpg, char* timestamp, char* file, char* size, off64_t filesize, char* logfile)
1955{
1956        char* cmd = NULL;
1957
1958        if(tmpjpg == NULL || tmpmpg == NULL) return 1;
1959
1960#ifdef MIPSEL
1961        cmd = ostrcat(cmd, "jpeg2yuv -v 1 -f 25 -n1 -I p -j ", 1, 0);
1962        cmd = ostrcat(cmd, tmpjpg, 1, 0);
1963        cmd = ostrcat(cmd, " | mpeg2enc -v 1 -x 1280 -y 720 -a 3 -f12 -4 1 -2 1 -q 1 -H -o ", 1, 0);
1964        cmd = ostrcat(cmd, tmpmpg, 1, 0);
1965#else
1966        cmd = ostrcat(cmd, "ffmpeg -y -f image2 -i ", 1, 0);
1967        cmd = ostrcat(cmd, tmpjpg, 1, 0);
1968        cmd = ostrcat(cmd, " ", 1, 0);
1969        cmd = ostrcat(cmd, tmpmpg, 1, 0);
1970#endif
1971
1972        if(logfile != NULL && getconfigint("mediadbdebug", NULL) == 1)
1973        {
1974                filedebug(logfile, "#############\nLocalfile: %s/%s_backdrop1.jpg size=(%s) filesize(%lld) (%s)\n#############", getconfig("mediadbpath", NULL), timestamp, size, filesize, file);
1975                cmd = ostrcat(cmd, " >> ", 1, 0);
1976                cmd = ostrcat(cmd, logfile, 1, 0);
1977                cmd = ostrcat(cmd, " 2>&1", 1, 0);
1978        }
1979        else
1980                cmd = ostrcat(cmd, " > /dev/null 2>&1", 1, 0);
1981
1982        debug(133, "cmd %s", cmd);
1983        system(cmd);
1984        free(cmd); cmd = NULL;
1985
1986        return 0;
1987}
1988
1989int mediadbcp(char* timestamp, char* poster)
1990{
1991        char* cmd = NULL;
1992
1993        if(timestamp == NULL || poster == NULL) return 1;
1994
1995        cmd = ostrcat(cmd, "cp -a ", 1, 0);
1996        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
1997        cmd = ostrcat(cmd, "/", 1, 0);
1998        cmd = ostrcat(cmd, timestamp, 1, 0);
1999        cmd = ostrcat(cmd, "_cover.jpg", 1, 0);
2000        cmd = ostrcat(cmd, " ", 1, 0);
2001        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2002        cmd = ostrcat(cmd, "/", 1, 0);
2003        cmd = ostrcat(cmd, timestamp, 1, 0);
2004        cmd = ostrcat(cmd, poster, 1, 0);
2005
2006        debug(133, "cmd %s", cmd);
2007        system(cmd);
2008        free(cmd); cmd = NULL;
2009
2010        return 0;
2011}
2012
2013int mediadbcprec(char* timestamp, char* poster)
2014{
2015        char* cmd = NULL;
2016
2017        if(timestamp == NULL || poster == NULL) return 1;
2018
2019        cmd = ostrcat(cmd, "cp -a /tmp/screenshot", 1, 0);
2020        cmd = ostrcat(cmd, poster, 1, 0);
2021        cmd = ostrcat(cmd, " ", 1, 0);
2022        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2023        cmd = ostrcat(cmd, "/", 1, 0);
2024        cmd = ostrcat(cmd, timestamp, 1, 0);
2025        cmd = ostrcat(cmd, poster, 1, 0);
2026
2027        debug(133, "cmd %s", cmd);
2028        system(cmd);
2029        free(cmd); cmd = NULL;
2030
2031        cmd = ostrcat(cmd, "rm -f /tmp/screenshot", 1, 0);
2032        cmd = ostrcat(cmd, poster, 1, 0);
2033        debug(133, "cmd %s", cmd);
2034        system(cmd);
2035        free(cmd); cmd = NULL;
2036       
2037        return 0;
2038}
2039
2040void mediadbcptmdb(struct tmdb* tmdb, struct imdb* imdb)
2041{
2042        if(tmdb == NULL || imdb == NULL) return;
2043
2044        if(tmdb->imdbid != NULL) imdb->id = ostrcat(tmdb->imdbid, NULL, 0, 0);
2045        if(tmdb->title != NULL) imdb->title = ostrcat(tmdb->title, NULL, 0, 0);
2046        if(tmdb->genre != NULL) imdb->genre = ostrcat(tmdb->genre, NULL, 0, 0);
2047        if(tmdb->rating != NULL) imdb->rating = ostrcat(tmdb->rating, NULL, 0, 0);
2048        if(tmdb->votes != NULL) imdb->votes = ostrcat(tmdb->votes, NULL, 0, 0);
2049        if(tmdb->runtime != NULL) imdb->runtime = ostrcat(tmdb->runtime, NULL, 0, 0);
2050        if(tmdb->plot != NULL) imdb->plot = ostrcat(tmdb->plot, NULL, 0, 0);
2051        if(tmdb->released != NULL) imdb->released = ostrcat(tmdb->released, NULL, 0, 0);
2052        if(tmdb->postermid != NULL) imdb->poster = ostrcat(tmdb->postermid, NULL, 0, 0);
2053        if(tmdb->thumb != NULL) imdb->thumb = ostrcat(tmdb->thumb, NULL, 0, 0);
2054        if(tmdb->year != NULL) imdb->year = ostrcat(tmdb->year, NULL, 0, 0);
2055}
2056
2057void mediadbcpimdbapi(struct imdbapi* imdbapi, struct imdb* imdb)
2058{
2059        if(imdbapi == NULL || imdb == NULL) return;
2060
2061        if(imdbapi->id != NULL) imdb->id = ostrcat(imdbapi->id, NULL, 0, 0);
2062        if(imdbapi->title != NULL) imdb->title = ostrcat(imdbapi->title, NULL, 0, 0);
2063        if(imdbapi->genre != NULL) imdb->genre = ostrcat(imdbapi->genre, NULL, 0, 0);
2064        if(imdbapi->writer != NULL) imdb->writer = ostrcat(imdbapi->writer, NULL, 0, 0);
2065        if(imdbapi->director != NULL) imdb->director = ostrcat(imdbapi->director, NULL, 0, 0);
2066        if(imdbapi->actors != NULL) imdb->actors = ostrcat(imdbapi->actors, NULL, 0, 0);
2067        if(imdbapi->rating != NULL) imdb->rating = ostrcat(imdbapi->rating, NULL, 0, 0);
2068        if(imdbapi->votes != NULL) imdb->votes = ostrcat(imdbapi->votes, NULL, 0, 0);
2069        if(imdbapi->runtime != NULL) imdb->runtime = ostrcat(imdbapi->runtime, NULL, 0, 0);
2070        if(imdbapi->plot != NULL) imdb->plot = ostrcat(imdbapi->plot, NULL, 0, 0);
2071        if(imdbapi->released != NULL) imdb->released = ostrcat(imdbapi->released, NULL, 0, 0);
2072        if(imdbapi->year != NULL) imdb->year = ostrcat(imdbapi->year, NULL, 0, 0);
2073}
2074
2075void mediadbcptmdb2(struct tmdb* tmdb, struct imdb* imdb)
2076{
2077        if(tmdb == NULL || imdb == NULL) return;
2078
2079        if(imdb->id == NULL) imdb->id = ostrcat(imdb->id, tmdb->imdbid, 1, 0);
2080        if(imdb->title == NULL) imdb->title = ostrcat(imdb->title, tmdb->title, 1, 0);
2081        if(imdb->genre == NULL) imdb->genre = ostrcat(imdb->genre, tmdb->genre, 1, 0);
2082        if(imdb->rating == NULL) imdb->rating = ostrcat(imdb->rating, tmdb->rating, 1, 0);
2083        if(imdb->votes == NULL) imdb->votes = ostrcat(imdb->votes, tmdb->votes, 1, 0);
2084        if(imdb->runtime == NULL) imdb->runtime = ostrcat(imdb->runtime, tmdb->runtime, 1, 0);
2085        if(imdb->plot == NULL) imdb->plot = ostrcat(imdb->plot, tmdb->plot, 1, 0);
2086        if(imdb->released == NULL) imdb->released = ostrcat(imdb->released, tmdb->released, 1, 0);
2087        if(imdb->poster == NULL) imdb->poster = ostrcat(imdb->poster, tmdb->postermid, 1, 0);
2088        if(imdb->thumb == NULL) imdb->thumb = ostrcat(imdb->thumb, tmdb->thumb, 1, 0);
2089        if(imdb->year == NULL) imdb->year = ostrcat(imdb->year, tmdb->year, 1, 0);
2090}
2091
2092void mediadbcpimdbapi2(struct imdbapi* imdbapi, struct imdb* imdb)
2093{
2094        if(imdbapi == NULL || imdb == NULL) return;
2095
2096        if(imdb->id == NULL) imdb->id = ostrcat(imdb->id, imdbapi->id, 1, 0);
2097        if(imdb->title == NULL) imdb->title = ostrcat(imdb->title, imdbapi->title, 1, 0);
2098        if(imdb->genre == NULL) imdb->genre = ostrcat(imdb->genre, imdbapi->genre, 1, 0);
2099        if(imdb->writer == NULL) imdb->writer = ostrcat(imdb->writer, imdbapi->writer, 1, 0);
2100        if(imdb->director == NULL) imdb->director = ostrcat(imdb->director, imdbapi->director, 1, 0);
2101        if(imdb->actors == NULL) imdb->actors = ostrcat(imdb->actors, imdbapi->actors, 1, 0);
2102        if(imdb->rating == NULL) imdb->rating = ostrcat(imdb->rating, imdbapi->rating, 1, 0);
2103        if(imdb->votes == NULL) imdb->votes = ostrcat(imdb->votes, imdbapi->votes, 1, 0);
2104        if(imdb->runtime == NULL) imdb->runtime = ostrcat(imdb->runtime, imdbapi->runtime, 1, 0);
2105        if(imdb->plot == NULL) imdb->plot = ostrcat(imdb->plot, imdbapi->plot, 1, 0);
2106        if(imdb->released == NULL) imdb->released = ostrcat(imdb->released, imdbapi->released, 1, 0);
2107        if(imdb->poster == NULL) imdb->poster = ostrcat(imdb->poster, imdbapi->poster, 1, 0);
2108        if(imdb->year == NULL) imdb->year = ostrcat(imdb->year, imdbapi->year, 1, 0);
2109}
2110
2111int mediadbjpegtran(char* tmpjpg, char* timestamp)
2112{
2113        char* cmd = NULL;
2114
2115        if(tmpjpg == NULL || timestamp == NULL) return 1;
2116
2117#ifdef MIPSEL
2118        cmd = ostrcat(cmd, "cp -a ", 1, 0);
2119        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2120        cmd = ostrcat(cmd, "/", 1, 0);
2121        cmd = ostrcat(cmd, timestamp, 1, 0);
2122        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2123        cmd = ostrcat(cmd, " ", 1, 0);
2124        cmd = ostrcat(cmd, tmpjpg, 1, 0);
2125#else
2126        cmd = ostrcat(cmd, "jpegtran -outfile ", 1, 0);
2127        cmd = ostrcat(cmd, tmpjpg, 1, 0);
2128        cmd = ostrcat(cmd, " -copy none ", 1, 0);
2129        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2130        cmd = ostrcat(cmd, "/", 1, 0);
2131        cmd = ostrcat(cmd, timestamp, 1, 0);
2132        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2133#endif
2134
2135        debug(133, "cmd %s", cmd);
2136        system(cmd);
2137        free(cmd); cmd = NULL;
2138
2139        return 0;
2140}
2141
2142int mediadbmv(char* tmpmpg, char* timestamp)
2143{
2144        char* cmd = NULL;
2145
2146        if(tmpmpg == NULL || timestamp == NULL) return 1;
2147
2148        cmd = ostrcat(cmd, "mv -f ", 1, 0);
2149        cmd = ostrcat(cmd, tmpmpg, 1, 0);
2150        cmd = ostrcat(cmd, " ", 1, 0);
2151        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2152        cmd = ostrcat(cmd, "/", 1, 0);
2153        cmd = ostrcat(cmd, timestamp, 1, 0);
2154        cmd = ostrcat(cmd, "_backdrop1.mvi", 1, 0);
2155
2156        debug(133, "cmd %s", cmd);
2157        system(cmd);
2158        free(cmd); cmd = NULL;
2159
2160        return 0;
2161}
2162
2163// flag 0 = autoscan
2164// flag 1 = manual scan imdb
2165// flag 2 = manual scan tmdb
2166// flag 3 = manual scan imdbapi
2167void mediadbfindfilecb(char* path, char* file, int type, char* id, int flag)
2168{
2169        debug(133, "path: %s",path);
2170        debug(133, "file: %s",file);
2171        debug(133, "type: %d", type);
2172        debug(133, "id: %s",id);
2173        debug(133, "flag: %d", flag);
2174       
2175        int isrec = 0;
2176        int iscam = 0;
2177        int backdrop = 0;
2178        char* shortpath = NULL, *tmpstr = NULL, *tmpid = NULL;
2179        struct mediadb *node = NULL, *cmediadb = NULL;
2180
2181        if(id != NULL)
2182                tmpid = ostrcat(tmpid, id, 1, 0);
2183               
2184        shortpath = delmountpart(path, 0);
2185        if(shortpath == NULL) return; //no mountpart found
2186
2187        //check if entry exist
2188        node = mediadbcheckfile(file, path, shortpath);
2189
2190        int tout = getconfigint("mediadbscantimeout", NULL);
2191
2192        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)))
2193        {
2194                if(type == 0)
2195                {               
2196                        struct imdb* imdb = NULL;
2197                        struct imdbapi* imdbapi = NULL;
2198                        struct tmdb* tmdb = NULL;
2199                       
2200                        char* shortname = createshortname(file, &isrec, &iscam, 1);
2201                        char* fileinfo = createshortname(file, &isrec, &iscam, 2);
2202
2203                        char* logdir = NULL, *logfile = NULL, *tmpjpg = NULL, *tmpmpg = NULL, *tmpmeta = NULL, *timen = NULL;
2204
2205                        logdir = ostrcat(getconfig("mediadbpath", NULL), "/.mediadbdebug", 0, 0);
2206                        if(getconfigint("mediadbscandelall", NULL) == 1)
2207                                unlink(logdir);
2208                               
2209                        if(!file_exist(logdir))
2210                                mkdir(logdir, 0777);
2211                        logfile = ostrcat(logdir, "/imdb-scan.log", 0, 0);
2212
2213                        timen = ostrcat(oitoa(time(NULL)), NULL, 1, 0);
2214                       
2215                        tmpjpg = ostrcat("/tmp/backdrop.resize.", timen, 0, 0);
2216                        tmpjpg = ostrcat(tmpjpg, ".jpg", 1, 0);
2217                       
2218                        tmpmpg = ostrcat("/tmp/backdrop.resize.", timen, 0, 0);
2219#ifdef MIPSEL
2220                        tmpmpg = ostrcat(tmpmpg, ".mvi", 1, 0);
2221#else
2222                        tmpmpg = ostrcat(tmpmpg, ".mpg", 1, 0);
2223#endif                 
2224                        tmpmeta = ostrcat("/tmp/mediadb.", timen, 0, 0);
2225                        tmpmeta = ostrcat(tmpmeta, ".meta", 1, 0);
2226                       
2227                        if(getconfigint("mediadbdebug", NULL) == 1)
2228                                filedebug(logfile, "#############\nfile: %s\nshortname: %s\nfileinfo: %s\n#############", file, shortname, fileinfo);
2229       
2230                        //got imdb infos
2231                        struct skin* imdbplugin = getplugin("IMDb");
2232                        if(imdbplugin != NULL)
2233                        {
2234                                struct imdb* (*startplugin)(struct imdb**, char*, int, int, int);
2235                                startplugin = dlsym(imdbplugin->pluginhandle, "getimdb");
2236                                if(startplugin != NULL)
2237                                {
2238                                        if((flag == 1) || (flag == 2))
2239                                        {
2240                                                tmpid = string_replace("tt", "", tmpid, 1);
2241                                                imdb = startplugin(&imdb, tmpid, 2, 0, 0); //load with imdbid with save
2242                                        }
2243                                        else
2244                                                imdb = startplugin(&imdb, shortname, 0, 1, 0);
2245                                }
2246                        }
2247
2248                        struct skin* imdbapiplugin = NULL;
2249                        struct skin* tmdbplugin = NULL;
2250
2251                        if(flag > 0 || (isrec == 0 && iscam == 0))
2252                        {
2253                                if(flag == 2 && imdb != NULL && id != NULL)
2254                                        imdb->id = ostrcat(id, NULL, 0, 0);
2255       
2256                                if(flag == 3 && imdb != NULL && id != NULL)
2257                                        imdb->id = ostrcat(id, NULL, 0, 0);
2258               
2259                                imdbapiplugin = getplugin("IMDb-API");
2260                                if(imdbplugin != NULL)
2261                                {
2262                                        struct imdbapi* (*startplugin)(struct imdbapi**, char*, int, int);
2263                                        startplugin = dlsym(imdbapiplugin->pluginhandle, "getimdbapi");
2264                                        if(startplugin != NULL)
2265                                        {
2266                                                if(imdb == NULL)
2267                                                        imdbapi = startplugin(&imdbapi, shortname, 0, 1);
2268                                                else if(imdb->id != NULL)
2269                                                        imdbapi = startplugin(&imdbapi, imdb->id, 1, 1);
2270                                        }
2271                                }
2272       
2273                                tmdbplugin = getplugin("TMDb");
2274                                if(tmdbplugin != NULL)
2275                                {
2276                                        struct tmdb* (*startplugin)(struct tmdb**, char*, int, int);
2277                                        startplugin = dlsym(tmdbplugin->pluginhandle, "gettmdb");
2278                                        if(startplugin != NULL)
2279                                        {
2280                                                if(imdb != NULL && imdb->id != NULL && flag > 0)
2281                                                        tmdb = startplugin(&tmdb, imdb->id, 1, 2);
2282                                                else if(imdbapi != NULL && imdbapi->id != NULL && flag > 0)
2283                                                        tmdb = startplugin(&tmdb, imdbapi->id, 1, 2);
2284                                                else if(imdb != NULL && imdb->id != NULL)
2285                                                        tmdb = startplugin(&tmdb, imdb->id, 1, 1);
2286                                                else if(imdbapi != NULL && imdbapi->id != NULL)
2287                                                        tmdb = startplugin(&tmdb, imdbapi->id, 1, 1);
2288                                        }
2289                                }
2290       
2291                                debugimdbnode(imdb);
2292                                if(flag == 2) mediadbcptmdb(tmdb, imdb); // manuel tmdb
2293                                debugimdbnode(imdb);
2294                                if(flag == 3) mediadbcpimdbapi(imdbapi, imdb); // manuel imdbapi
2295                                mediadbcptmdb2(tmdb, imdb);
2296                                debugimdbnode(imdb);
2297                                mediadbcpimdbapi2(imdbapi, imdb);
2298
2299                                if(tmdb != NULL && tmdb->mvi != NULL) backdrop = atoi(tmdb->mvi);
2300                        }
2301                        else if((cmpfilenameext(file, ".ts") == 0) || (cmpfilenameext(file, ".mts") == 0))
2302                        {
2303                                char* poster = NULL, *plot = NULL, *timestamp = NULL, *cmd = NULL, *tmpstr1 = NULL;
2304
2305                                timestamp = oregex(".*([0-9]{14,14}).*", file);
2306                                if(timestamp == NULL)
2307                                        timestamp = ostrcat(oitoa(time(NULL)), NULL, 1, 0);
2308                               
2309                                shortname = string_replace("   ", " - ", shortname, 1);
2310                                shortname = string_replace("  ", " - ", shortname, 1);
2311
2312                                poster = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2313                                poster = ostrcat(poster, timestamp, 1, 0);
2314                                poster = ostrcat(poster, "_poster.jpg", 1, 0);
2315
2316                                tmpstr = ostrcat(tmpstr, path, 1, 0);
2317                                tmpstr = ostrcat(tmpstr, "/", 1, 0);
2318                                tmpstr = ostrcat(tmpstr, file, 1, 0);
2319                                tmpstr1 = changefilenameext(tmpstr, ".epg");
2320                                free(tmpstr); tmpstr = NULL;
2321                                cmd = readfiletomem(tmpstr1, 1);
2322                                free(tmpstr1); tmpstr1 = NULL;
2323                                plot = ostrcat(plot, cmd, 1, 0);
2324                                free(cmd); cmd = NULL;
2325
2326                                if(file_exist("/tmp/screenshot_backdrop1.jpg"))
2327                                        mediadbcprec(timestamp, "_backdrop1.jpg");
2328                                else
2329                                        mediadbffmpeg1(file, path, timestamp, logfile);
2330
2331// bilder alle unscharf
2332/*
2333                                int channels = 0;
2334                                unsigned long width = 0, height = 0, rowbytes = 0;
2335                                unsigned char* buf = NULL;
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                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2342                                free(cmd), cmd = NULL;
2343                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2344                                cmd = ostrcat(cmd, timestamp, 1, 0);
2345                                cmd = ostrcat(cmd, "_thumb.jpg", 1, 0);
2346                                buf = savejpg(cmd, width, height, channels, 91, 140, 70, buf);
2347                                free(cmd); cmd = NULL;
2348                                free(buf); buf = NULL;
2349
2350                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2351                                cmd = ostrcat(cmd, "/", 1, 0);
2352                                cmd = ostrcat(cmd, timestamp, 1, 0);
2353                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2354                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2355                                free(cmd), cmd = NULL;
2356                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2357                                cmd = ostrcat(cmd, timestamp, 1, 0);
2358                                cmd = ostrcat(cmd, "_cover.jpg", 1, 0);
2359                                buf = savejpg(cmd, width, height, channels, 185, 264, 70, buf);
2360                                free(cmd); cmd = NULL;
2361                                free(buf); buf = NULL;
2362
2363                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2364                                cmd = ostrcat(cmd, "/", 1, 0);
2365                                cmd = ostrcat(cmd, timestamp, 1, 0);
2366                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2367                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2368                                free(cmd), cmd = NULL;
2369                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2370                                cmd = ostrcat(cmd, timestamp, 1, 0);
2371                                cmd = ostrcat(cmd, "_poster.jpg", 1, 0);
2372                                buf = savejpg(cmd, width, height, channels, 400, 450, 70, buf);
2373                                free(cmd); cmd = NULL;
2374                                free(buf); buf = NULL;
2375
2376                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2377                                cmd = ostrcat(cmd, "/", 1, 0);
2378                                cmd = ostrcat(cmd, timestamp, 1, 0);
2379                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2380                                buf = loadjpg(cmd, &width, &height, &rowbytes, &channels, 16);
2381                                free(cmd), cmd = NULL;
2382                                cmd = ostrcat(getconfig("mediadbpath", NULL), "/", 0, 0);
2383                                cmd = ostrcat(cmd, timestamp, 1, 0);
2384                                cmd = ostrcat(cmd, "_postermid.jpg", 1, 0);
2385                                buf = savejpg(cmd, width, height, channels, 400, 450, 70, buf);
2386                                free(cmd); cmd = NULL;
2387                                free(buf); buf = NULL;
2388*/
2389
2390                                if(file_exist("/tmp/screenshot_thumb.jpg"))
2391                                        mediadbcprec(timestamp, "_thumb.jpg");
2392                                else
2393                                        mediadbffmpeg2(file, path, timestamp, logfile);
2394
2395                                if(file_exist("/tmp/screenshot_cover.jpg"))
2396                                        mediadbcprec(timestamp, "_cover.jpg");
2397                                else
2398                                        mediadbffmpeg3(file, path, timestamp, logfile);
2399
2400                                mediadbcp(timestamp, "_poster.jpg");
2401                                mediadbcp(timestamp, "_postermid.jpg");
2402
2403                                cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2404                                cmd = ostrcat(cmd, "/", 1, 0);
2405                                cmd = ostrcat(cmd, timestamp, 1, 0);
2406                                cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2407                               
2408                                off64_t filesize = getfilesize(cmd);
2409                                debug(133, "filesize %lld", filesize);
2410                                free(cmd); cmd = NULL;
2411                       
2412                                if(filesize < 1500000)
2413                                {
2414                                        char* size = NULL;
2415#ifndef MIPSEL
2416                                        mediadbffmpeg4(timestamp, tmpmeta);
2417                                               
2418                                        cmd = ostrcat(cmd, "cat ", 1, 0);
2419                                        cmd = ostrcat(cmd, tmpmeta, 1, 0);
2420                                        cmd = ostrcat(cmd, " | grep Stream | awk '{print $6}' | cut -d'x' -f1", 1, 0);
2421                                        size = string_newline(command(cmd));
2422                                        free(cmd); cmd= NULL;
2423                                        debug(133, "size %s", size);
2424                                        if(size != NULL)
2425                                        {
2426                                                debug(133, "size %d", atoi(size));
2427                                                int picsize = atoi(size);
2428       
2429                                                if(picsize < 2000)
2430                                                {
2431                                                        debug(133, "size ok %d", picsize);
2432#endif
2433                                                        mediadbjpegtran(tmpjpg, timestamp);
2434                                               
2435                                                        if(file_exist(tmpjpg))
2436                                                        {
2437                                                                mediadbffmpeg5(tmpjpg, tmpmpg, timestamp, file, size, filesize, logfile);
2438
2439                                                                if(file_exist(tmpmpg))
2440                                                                {
2441                                                                        mediadbmv(tmpmpg, timestamp);
2442                                                                        backdrop = 1;
2443                                                                        writesysint("/proc/sys/vm/drop_caches", 3, 0);
2444
2445                                                                        debug(777, "imdb id %s", timestamp);
2446                                                                        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);
2447                                                                }       
2448                                                        }
2449#ifndef MIPSEL
2450                                                }
2451                                                else
2452                                                {
2453                                                        debug(133, "ERROR Localfile size to big skipped %d", picsize);
2454       
2455                                                        if(getconfigint("mediadbdebug", NULL) == 1)
2456                                                                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);
2457                                                }
2458                                        }
2459                                        else
2460                                        {
2461                                                debug(133, "ERROR Localfile size is NULL skipped %s", size);
2462       
2463                                                if(getconfigint("mediadbdebug", NULL) == 1)
2464                                                        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);
2465                                        }
2466                                        free(size); size = NULL;
2467#endif
2468                                        unlink(tmpmeta);
2469                                        unlink(tmpjpg);
2470                                        unlink(tmpmpg);
2471                                       
2472                                        cmd = ostrcat(cmd, getconfig("mediadbpath", NULL), 1, 0);
2473                                        cmd = ostrcat(cmd, "/", 1, 0); 
2474                                        cmd = ostrcat(cmd, timestamp, 1, 0);
2475                                        cmd = ostrcat(cmd, "_backdrop1.jpg", 1, 0);
2476                                        unlink(cmd);
2477                                }
2478                                else
2479                                {
2480                                        debug(133, "ERROR Localfile filesize to BIG skipped %lld", filesize);
2481               
2482                                        if(getconfigint("mediadbdebug", NULL) == 1)
2483                                                filedebug(logfile, "#############\nERROR Localfile filesize to BIG skipped: %s/%s_backdrop1.jpg filesize(%lld) (%s)\n#############", getconfig("mediadbpath", NULL), timestamp, filesize, file);
2484                                }
2485                                free(cmd); cmd = NULL;
2486                                free(timestamp); timestamp = NULL;
2487                                free(poster); poster = NULL;
2488                                free(plot); plot = NULL;
2489                        }
2490               
2491                        free(logdir); logdir = NULL;
2492                        free(logfile); logfile = NULL;
2493                        free(timen); timen = NULL;
2494                        free(tmpjpg); tmpjpg = NULL;
2495                        free(tmpmpg); tmpmpg = NULL;
2496                        free(tmpmeta); tmpmeta = NULL;
2497
2498                        debugimdbnode(imdb);
2499                       
2500                        if(cmediadb == NULL)
2501                        {
2502                                debug(777, "add video: %s/%s", shortpath, file);
2503                                if(imdb != NULL)
2504                                {
2505                                        debug(777, "imdb id %s", imdb->id);
2506                                        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);
2507                                        if(tmpid != NULL)
2508                                        {
2509                                                char* tmpstr = NULL;
2510                                                tmpstr = ostrcat(tmpstr, _("file"), 1, 0);
2511                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2512                                                tmpstr = ostrcat(tmpstr, file, 1, 0);                                                   
2513                                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
2514                                                tmpstr = ostrcat(tmpstr, _("path"), 1, 0);
2515                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2516                                                tmpstr = ostrcat(tmpstr, path, 1, 0);                                                   
2517                                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
2518                                                tmpstr = ostrcat(tmpstr, _("Title"), 1, 0);
2519                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2520                                                tmpstr = ostrcat(tmpstr, imdb->title, 1, 0);                                                   
2521                                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
2522                                                tmpstr = ostrcat(tmpstr, _("imdbid"), 1, 0);
2523                                                tmpstr = ostrcat(tmpstr, ": ", 1, 0);
2524                                                tmpstr = ostrcat(tmpstr, id, 1, 0);                                                                                                                                                                                                             
2525                                                textbox(_("Add iMDB manuel"), tmpstr, _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 500, 10, 0);
2526                                                free(tmpstr); tmpstr = NULL;
2527                                        }
2528                                }
2529                                else
2530                                        cmediadb = createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, shortname, fileinfo, 0, 0);
2531                        }
2532
2533                        debug(777, "shortname: %s", shortname);
2534                        debug(133, "shortname: %s", shortname);
2535                        free(shortname); shortname = NULL;
2536
2537                        debug(777, "fileinfo: %s", fileinfo);
2538                        debug(133, "fileinfo: %s", fileinfo);
2539                        free(fileinfo); fileinfo = NULL;
2540                                               
2541                        if(imdbplugin != NULL)
2542                        {
2543                                void (*startplugin)(struct imdb**, int flag);
2544                                startplugin = dlsym(imdbplugin->pluginhandle, "freeimdb");
2545                                if(startplugin != NULL)
2546                                        startplugin(&imdb, 0);
2547                        }
2548                        imdb = NULL;
2549                       
2550                        if(imdbapiplugin != NULL)
2551                        {
2552                                void (*startplugin)(struct imdbapi**, int);
2553                                startplugin = dlsym(imdbapiplugin->pluginhandle, "freeimdbapi");
2554                                if(startplugin != NULL)
2555                                        startplugin(&imdbapi, 0);
2556                        }
2557                        imdbapi = NULL;
2558
2559                        if(tmdbplugin != NULL)
2560                        {
2561                                void (*startplugin)(struct tmdb**, int);
2562                                startplugin = dlsym(tmdbplugin->pluginhandle, "freetmdb");
2563                                if(startplugin != NULL)
2564                                        startplugin(&tmdb, 0);
2565                        }
2566                        tmdb = NULL;
2567                }
2568                else if(type == 1)
2569                {
2570                        struct id3tag* id3tag = NULL;
2571
2572                        debug(777, "add audio: %s/%s", shortpath, file);
2573
2574                        // create filelist info
2575                        free(tmpstr), tmpstr = NULL;
2576                        tmpstr = ostrcat(tmpstr, file, 1, 0);
2577                        string_tolower(tmpstr);
2578                        char* fileinfo = NULL;
2579                        fileinfo = ostrcat(fileinfo, getfilenameext(tmpstr), 1, 1);
2580                        free(tmpstr), tmpstr = NULL;
2581
2582                        char* tmpfile = ostrcat(path, "/", 0, 0);
2583                        tmpfile = ostrcat(tmpfile, file, 1, 0);
2584
2585                        int hash = gethash(tmpfile);
2586                        char* tmphash = olutoa(hash);
2587
2588                        id3tag = getid3(tmpfile, tmphash, 1);
2589
2590                        if(id3tag != NULL)
2591                        {
2592                                if(id3tag->poster != NULL)
2593                                        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);
2594                                else
2595                                        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);
2596                        }
2597                        else
2598                                cmediadb = createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, NULL, NULL, 0, 0);
2599
2600                        free(tmpfile); tmpfile = NULL;
2601                        free(tmphash); tmphash = NULL;
2602                        free(fileinfo); fileinfo = NULL;
2603                        freeid3(id3tag); id3tag = NULL;
2604                }
2605                else if(type == 2)
2606                {
2607                        char* thumbfile = NULL;
2608
2609                        debug(777, "add pic: %s/%s", shortpath, file);
2610                        if(status.thumbthread != NULL)
2611                        {
2612                                //check if thumb exists
2613                                thumbfile = checkthumb(path, file);
2614                                if(thumbfile == NULL)
2615                                        addqueue(101, (void*)path, strlen(path) + 1, (void*)file, strlen(file) + 1, 0, NULL);
2616                        }
2617                        free(thumbfile); thumbfile = NULL;
2618
2619                        cmediadb = createmediadb(node, NULL, type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, shortpath, file, NULL, NULL, 0, 0);
2620                }
2621        }
2622        free(shortpath); shortpath = NULL;
2623
2624        //first time save full mediadb, rest save only akt entry
2625        if(status.mediadbsavetime == 0)
2626        {
2627                writemediadb(getconfig("mediadbfile", NULL), NULL);
2628                status.mediadbsavetime = 1;
2629        }
2630        else if(cmediadb != NULL)
2631                writemediadb(getconfig("mediadbfile", NULL), cmediadb);
2632}
2633
2634int findfiles(char* dirname, int type, int onlydir, int onlycount, int first)
2635{
2636        debug(777, "dir=%s type=%d onlydir=%d, onlycount=%d", dirname, type, onlydir, onlycount);
2637        DIR *d;
2638        char* tmpstr = NULL;
2639
2640        //Open the directory specified by dirname
2641        d = opendir(dirname);
2642
2643        //Check it was opened
2644        if(! d)
2645        {
2646                perr("Cannot open directory %s", dirname);
2647                return -1;
2648        }
2649
2650        int count = 0;
2651
2652        while(1)
2653        {
2654                struct dirent* entry;
2655                int path_length;
2656                char path[PATH_MAX];
2657
2658                snprintf(path, PATH_MAX, "%s", dirname);
2659                //Readdir gets subsequent entries from d
2660                entry = readdir(d);
2661
2662                if(!entry) //There are no more entries in this directory, so break out of the while loop
2663                        break;
2664
2665                //for nfs mounts if file type is unknown use stat
2666                if(entry->d_type == DT_UNKNOWN)
2667                {
2668                        tmpstr = ostrcat(dirname, "/", 0, 0);
2669                        tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0);
2670                        entry->d_type = getfiletype(tmpstr);
2671                        free(tmpstr); tmpstr = NULL;
2672                }
2673
2674                //check if link is a dir
2675                if(first == 1 && entry->d_type == DT_LNK)
2676                {
2677                        tmpstr = createpath(path, entry->d_name);
2678                        if(isdir(tmpstr) == 1)
2679                                entry->d_type = DT_DIR;
2680
2681                        free(tmpstr); tmpstr = NULL;
2682                }
2683
2684                //See if entry is a subdirectory of d
2685                if(entry->d_type == DT_DIR)
2686                {
2687                        //Check that the directory is not d or d's parent
2688                        if(entry->d_name != NULL && entry->d_name[0] != '.' && entry->d_name[0] != '$')
2689                        {
2690                                path_length = snprintf(path, PATH_MAX, "%s/%s", dirname, entry->d_name);
2691                                if(path_length >= PATH_MAX)
2692                                {
2693                                        err("path length has got too long");
2694                                        if(d) closedir(d);
2695                                        return -1;
2696                                }
2697                                //Recursively call findfiles with the new path
2698                                if(onlydir == 0)
2699                                        findfiles(path, type, onlydir, onlycount, 0);
2700                        }
2701                }
2702                else //File
2703                {
2704                        //TODO: add extensions
2705                       
2706                        if(file_exist("/mnt/swapextensions/etc/.codecpack") || file_exist("/var/swap/etc/.codecpack") || file_exist("/var/etc/.codecpack"))
2707                        {
2708                                if(!filelistflt(".avi .dat .divx .flv .mkv .m4v .mp4 .mov .mpg .mpeg .mts .m2ts .trp .ts .vdr .vob .wmv .rm", entry->d_name)) //video
2709                                {                               
2710                                        if(type == 0 || type == 100 || type == 90 || type == 91)
2711                                        {                                       
2712                                                if(onlycount == 0)
2713                                                        mediadbfindfilecb(path, entry->d_name, 0, NULL, 0);
2714                                                else
2715                                                        count += 1;
2716                                        }
2717                                }
2718                                else if(!filelistflt(".mp3 .flac .ogg .wma .ra .wav", entry->d_name)) //audio
2719                                {
2720                                        if(type == 1 || type == 100 || type == 90 || type == 92)
2721                                        {
2722                                                if(onlycount == 0)
2723                                                        mediadbfindfilecb(path, entry->d_name, 1, NULL, 0);
2724                                                else
2725                                                        count += 1;
2726                                        }
2727                                }
2728                                else if(!filelistflt(".jpg .png", entry->d_name)) //picture
2729                                {
2730                                        if(type == 2 || type == 100 || type == 91 || type == 92)
2731                                        {
2732                                                if(onlycount == 0)
2733                                                        mediadbfindfilecb(path, entry->d_name, 2, NULL, 0);
2734                                                else
2735                                                        count += 1;
2736                                        }
2737                                }
2738                        }
2739                        else
2740                        {
2741                                if(!filelistflt(".avi .mkv .mpg .mpeg .ts", entry->d_name)) //video
2742                                {                               
2743                                        if(type == 0 || type == 100 || type == 90 || type == 91)
2744                                        {
2745                                                if(onlycount == 0)
2746                                                        mediadbfindfilecb(path, entry->d_name, 0, NULL, 0);
2747                                                else
2748                                                        count += 1;
2749                                        }
2750                                }
2751                                else if(!filelistflt(".mp3 .flac .ogg", entry->d_name)) //audio
2752                                {
2753                                        if(type == 1 || type == 100 || type == 90 || type == 92)
2754                                        {
2755                                                if(onlycount == 0)
2756                                                        mediadbfindfilecb(path, entry->d_name, 1, NULL, 0);
2757                                                else
2758                                                        count += 1;
2759                                        }
2760                                }
2761                                else if(!filelistflt(".jpg .png", entry->d_name)) //picture
2762                                {
2763                                        if(type == 2 || type == 100 || type == 91 || type == 92)
2764                                        {
2765                                                if(onlycount == 0)
2766                                                        mediadbfindfilecb(path, entry->d_name, 2, NULL, 0);
2767                                                else
2768                                                        count += 1;
2769                                        }
2770                                }
2771                        }
2772                }
2773        }
2774
2775        //After going through all the entries, close the directory
2776        if(d && closedir(d))
2777        {
2778                perr("Could not close %s", dirname);
2779                return -1;
2780        }
2781
2782        if(onlycount == 1)
2783                return count;
2784
2785        return 0;
2786}
2787
2788//type 0=video, 1=audio, 2=pic, 90=video/audio, 91=video/pic, 92=audio/pic, 100=all
2789//flag: 0 = scan recursive
2790//flag: 1 = not scan recursive
2791void mediadbscan(char* path, int type, int flag)
2792{
2793        int count = 0;
2794        if(flag == 1) type = type | 0x80000000;
2795
2796        //param1 (path) is freed in thread
2797        addtimer(&mediadbscanthread, START, 1000, 1, (void*)ostrcat(path, NULL, 0, 0), (void*)type, NULL);
2798
2799        //block a little
2800        while(status.mediadbthread != NULL && count < 20)
2801        {
2802                usleep(100000);
2803                count++;
2804        }
2805}
2806
2807#endif
Note: See TracBrowser for help on using the repository browser.