File: menu_dirselect.c

package info (click to toggle)
vdr-plugin-epgsearch 2.4.0%2Bgit20191101-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,992 kB
  • sloc: ansic: 22,373; perl: 1,330; sh: 630; makefile: 226
file content (267 lines) | stat: -rw-r--r-- 8,298 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*                                                                  -*- c++ -*-
Copyright (C) 2004-2013 Christian Wieninger

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

The author can be reached at cwieninger@gmx.de

The project's page is at http://winni.vdr-developer.org/epgsearch
*/

#include "menu_dirselect.h"
#include "epgsearchext.h"
#include "epgsearchcats.h"
#include "epgsearchtools.h"

set<string> cMenuDirSelect::directorySet;

cDirExts DirExts;
cConfDDirExts ConfDDirExts;

// --- cMenuDirItem ---------------------------------------------------------
class cMenuDirItem : public cOsdItem
{
private:
    char* directory;
public:
    cMenuDirItem(const char* text) : cOsdItem(text) {
        directory = strdup(text);
    }
    ~cMenuDirItem() {
        if (directory) free(directory);
    }
    virtual int Compare(const cListObject &ListObject) const;
};

int cMenuDirItem::Compare(const cListObject &ListObject) const
{
    const cMenuDirItem *p = (cMenuDirItem *)&ListObject;
    int hasVars1 = (strchr(directory, '%') != NULL ? 1 : 0);
    int hasVars2 = (strchr(p->directory, '%') != NULL ? 1 : 0);
    if (hasVars1 || hasVars2) {
        if (hasVars1 != hasVars2)
            return hasVars2 - hasVars1;
        else
            return strcasecmp(directory, p->directory);
    } else
        return strcasecmp(directory, p->directory);
}


// --- cMenuDirSelect ---------------------------------------------------------

cMenuDirSelect::cMenuDirSelect(char* szDirectory)
    : cOsdMenu(tr("Select directory"))
{
    SetMenuCategory(mcTimerEdit);

    Directory = szDirectory;
    yellow = NULL;
    MaxLevel = 1;
    CurLevel = 1;
    Load();
}

cMenuDirSelect::~cMenuDirSelect()
{
    if (yellow) free(yellow);
}

int cMenuDirSelect::Level(const char* szDir)
{
    int iLevel = 1;
    if (strchr(szDir, '%')) // dirs with vars always have level 1
        return 1;
    do {
        const char* pos = strchr(szDir, '~');
        if (pos) {
            iLevel++;
            szDir = pos + 1;
        } else
            return iLevel;
    } while (true);
    return 1;
}

void cMenuDirSelect::AddDistinct(const char* szText)
{
    int iLevel = Level(szText);
    MaxLevel = std::max(MaxLevel, iLevel);

    if (iLevel > CurLevel) // only show Items of the specified level, except those with vars
        return;

    for (int i = 0; i < Count(); i++) {
        const char* ItemText = Get(i)->Text();
        char* itemtext = strdup(ItemText);
        char* sztext = strdup(szText);
        ToLower(itemtext);
        ToLower(sztext);
        if (itemtext && strlen(itemtext) > 0 && strcmp(sztext, itemtext) == 0) {
            free(itemtext);
            free(sztext);
            return;
        }
        free(itemtext);
        free(sztext);
    }
    Add(new cMenuDirItem(hk(szText)));
}

void cMenuDirSelect::CreateDirSet(bool extraDirs)
{
    directorySet.clear();

    // add distinct directories from current recordings
    {
        LOCK_RECORDINGS_READ;
        for (const cRecording *recording = Recordings->First(); recording; recording = Recordings->Next(recording)) {
            if (recording->HierarchyLevels() > 0) {
                char* dir = strdup(recording->Name());
                // strip the trailing rec dir
                char* pos = strrchr(dir, '~');
                if (pos) {
                    *pos = 0;
                    for (int iLevel = 0; iLevel < recording->HierarchyLevels(); iLevel++) {
                        directorySet.insert(dir);
                        char* pos = strrchr(dir, '~');
                        if (pos)
                            *pos = 0;
                    }
                }
                free(dir);
            }
        }
    } // give up Recordings Lock
    // add distinct directories from current timers
    {
        LOCK_TIMERS_READ;
        for (const cTimer *timer = Timers->First(); timer; timer = Timers->Next(timer)) {
            char* dir = strdup(timer->File());
            // strip the trailing name dir
            char* pos = strrchr(dir, '~');
            if (pos) {
                *pos = 0;
                do {
                    directorySet.insert(dir);
                    char* pos = strrchr(dir, '~');
                    if (pos)
                        *pos = 0;
                    else break;
                } while (true);
            }
            free(dir);
        }
    }

    // add distinct directories from folders.conf
    for (cNestedItem* item = Folders.First(); item; item = Folders.Next(item))
        AddVDRFolders(item);

    if (extraDirs) {
        cMutexLock SearchExtsLock(&SearchExts);
        cSearchExt *searchExt = SearchExts.First();
        // add distinct directories from existing search timers
        while (searchExt) {
            if (strlen(searchExt->directory) > 0)
                directorySet.insert(searchExt->directory);
            searchExt = SearchExts.Next(searchExt);
        }
        // add distinct directories from epgsearchdirs.conf
        DirExts.Load(AddDirectory(CONFIGDIR, "epgsearchdirs.conf"), true);
        cDirExt* DirExt = DirExts.First();
        while (DirExt) {
            directorySet.insert(DirExt->Name());
            DirExt = DirExts.Next(DirExt);
        }
        // add distinct directories from conf.d files
        DirExt = ConfDDirExts.First();
        while (DirExt) {
            directorySet.insert(DirExt->Name());
            DirExt = ConfDDirExts.Next(DirExt);
        }
    }
}

void cMenuDirSelect::AddVDRFolders(cNestedItem* folder, string parentDirectory)
{
    if (folder == NULL) return;
    string folderDirectory = string((parentDirectory.size() == 0) ? "" : parentDirectory + "~") + folder->Text();
    directorySet.insert(folderDirectory);
    if (folder->SubItems() == NULL) return;
    for (cNestedItem* subfolder = folder->SubItems()->First(); subfolder; subfolder = folder->SubItems()->Next(subfolder))
        AddVDRFolders(subfolder, folderDirectory);
}

void cMenuDirSelect::Load()
{
    int current = Current();
    char* oldSelection = NULL; // save old selection for reselection
    if (current > -1)
        oldSelection = strdup(Get(current)->Text());
    Clear();

    CreateDirSet();
    std::set<string>::iterator it;
    for (it = directorySet.begin(); it != directorySet.end(); ++it)
        AddDistinct((*it).c_str());

    Sort();
    for (int i = 0; i < Count(); i++) {
        const char* text = Get(i)->Text();
        if (oldSelection && strchr(text, '%') == NULL && strstr(text, oldSelection) == text) { // skip entries with variables
            SetCurrent(Get(i));
            break;
        }
    }
    if (oldSelection) free(oldSelection);

    if (yellow) {
        free(yellow);
        yellow = NULL;
    }
    msprintf(&yellow, "%s %d", tr("Button$Level"), (CurLevel == MaxLevel ? 1 : CurLevel + 1));
    SetHelp(NULL, NULL, MaxLevel == 1 ? NULL : yellow, tr("Button$Select"));
    Display();
}

eOSState cMenuDirSelect::ProcessKey(eKeys Key)
{
    eOSState state = cOsdMenu::ProcessKey(Key);

    if (state == osUnknown) {
        switch ((int)Key) {
        case kBlue|k_Repeat:
        case kYellow:
            if (++CurLevel > MaxLevel)
                CurLevel = 1;
            Load();
            return osContinue;
        case kGreen:
        case kRed:
            return osContinue;
        case kBlue:
        case kOk:
            if (Count() > 0)
                strn0cpy(Directory, Get(Current())->Text(), MaxFileName);
            return osBack;
        default:
            break;
        }
    }
    return state;
}