summaryrefslogtreecommitdiff
path: root/menu.c
blob: a605d823c6d8f28dd8a8631c73548767284fb4a6 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * menu.c: Menu implementation for duplicates plugin.
 *
 * The menu implementation is based on recordings menu in VDR.
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "menu.h"
#include "visibility.h"
#include <vdr/menu.h>
#include <vdr/status.h>
#include <vdr/interface.h>
#include <sys/time.h>
#include <string>
#include <sstream>

static inline cOsdItem *SeparatorItem(const char *Label) {
  cOsdItem *Item = new cOsdItem(cString::sprintf("----- %s -----", Label));
  Item->SetSelectable(false);
  return Item;
}

// --- cDuplicatesReplayControl -------------------------------------------------------

class cDuplicatesReplayControl : public cReplayControl {
public:
  virtual eOSState ProcessKey(eKeys Key);
};

eOSState cDuplicatesReplayControl::ProcessKey(eKeys Key) {
  eOSState state = cReplayControl::ProcessKey(Key);
  if (state == osRecordings) {
     cControl::Shutdown();
     cRemote::CallPlugin("duplicates");
     return osContinue;
  }
  return state;
}

// --- cMenuDuplicate --------------------------------------------------------

class cMenuDuplicate : public cOsdMenu {
private:
  const cRecording *recording;
public:
  cMenuDuplicate(const cRecording *Recording);
  virtual void Display(void);
  virtual eOSState ProcessKey(eKeys Key);
};

cMenuDuplicate::cMenuDuplicate(const cRecording *Recording)
:cOsdMenu(trVDR("Recording info")) {
#if VDRVERSNUM >= 10728
  SetMenuCategory(mcRecording);
#endif
  recording = Recording;
  SetHelp(trVDR("Button$Play"));
}

void cMenuDuplicate::Display(void) {
  cOsdMenu::Display();
  DisplayMenu()->SetRecording(recording);
  if (recording->Info()->Description())
     cStatus::MsgOsdTextItem(recording->Info()->Description());
}

eOSState cMenuDuplicate::ProcessKey(eKeys Key)
{
  switch (int(Key)) {
    case kUp|k_Repeat:
    case kUp:
    case kDown|k_Repeat:
    case kDown:
    case kLeft|k_Repeat:
    case kLeft:
    case kRight|k_Repeat:
    case kRight:
                  DisplayMenu()->Scroll(NORMALKEY(Key) == kUp || NORMALKEY(Key) == kLeft, NORMALKEY(Key) == kLeft || NORMALKEY(Key) == kRight);
                  cStatus::MsgOsdTextItem(NULL, NORMALKEY(Key) == kUp || NORMALKEY(Key) == kLeft);
                  return osContinue;
    case kInfo:   return osBack;
    default: break;
}

  eOSState state = cOsdMenu::ProcessKey(Key);

  if (state == osUnknown) {
     switch (Key) {
       case kPlay:
       case kRed:    cRemote::Put(Key, true);
       case kOk:     return osBack;
       default: break;
     }
  }
  return state;
}

// --- cDuplicateRecording -------------------------------------------------------

class cDuplicateRecording : public cListObject {
private:
  const cRecording *recording;
  bool checked;
  cVisibility visibility;
  std::string title;
  std::string description;
public:
  cDuplicateRecording(const cRecording *Recording);
  cDuplicateRecording(const cDuplicateRecording &DuplicateRecording);
  const cRecording *Recording(void) const { return recording; }
  bool HasDescription(void) const { return ! description.empty(); }
  bool IsDuplicate(cDuplicateRecording *DuplicateRecording);
  void SetChecked(bool chkd = true) { checked = chkd; }
  bool Checked() { return checked; }
  cVisibility Visibility() { return visibility; }
};

cDuplicateRecording::cDuplicateRecording(const cRecording *Recording) : visibility(Recording->FileName()) {
  recording = Recording;
  checked = false;
  if (dc.title && recording->Info()->Title())
     title = std::string(recording->Info()->Title());
  else
     title = std::string();
  std::stringstream desc;
  if (recording->Info()->ShortText())
     desc << std::string(recording->Info()->ShortText());
  if (recording->Info()->Description())
     desc << std::string(recording->Info()->Description());
  description = desc.str();
  while(true) {
    size_t found = description.find("|");
    if (found == std::string::npos)
       break;
    description.replace(found, 1, "");
  }
  while(true) {
    size_t found = description.find(" ");
    if (found == std::string::npos)
       break;
    description.replace(found, 1, "");
  }
}

cDuplicateRecording::cDuplicateRecording(const cDuplicateRecording &DuplicateRecording) :
  recording(DuplicateRecording.recording),
  checked(DuplicateRecording.checked),
  visibility(DuplicateRecording.visibility),
  title(DuplicateRecording.title),
  description(DuplicateRecording.description) {}

bool cDuplicateRecording::IsDuplicate(cDuplicateRecording *DuplicateRecording) {
  if (!HasDescription() || !DuplicateRecording->HasDescription())
    return false;

  size_t found;
  if (dc.title) {
    found = title.size() > DuplicateRecording->title.size() ?
              title.find(DuplicateRecording->title) : DuplicateRecording->title.find(title);
    if (found == std::string::npos)
      return false;
  }

  found = description.size() > DuplicateRecording->description.size() ?
            description.find(DuplicateRecording->description) : DuplicateRecording->description.find(description);
  if (found != std::string::npos)
    return true;

  return false;
}

// --- cMenuDuplicateItem ----------------------------------------------------

class cMenuDuplicateItem : public cOsdItem {
private:
  char *fileName;
  cVisibility visibility;
public:
  cMenuDuplicateItem(cDuplicateRecording *DuplicateRecording);
  ~cMenuDuplicateItem();
  const char *FileName(void) { return fileName; }
  cVisibility Visibility() { return visibility; }
};

cMenuDuplicateItem::cMenuDuplicateItem(cDuplicateRecording *DuplicateRecording) : visibility(DuplicateRecording->Visibility()) {
  fileName = strdup(DuplicateRecording->Recording()->FileName());
#if defined LIEMIKUUTIO && LIEMIKUUTIO < 131
  SetText(DuplicateRecording->Recording()->Title('\t', true, -1, false));
#else
  SetText(DuplicateRecording->Recording()->Title('\t', true));
#endif
}

cMenuDuplicateItem::~cMenuDuplicateItem() {
  free(fileName);
}

// --- cMenuDuplicates -------------------------------------------------------

cMenuDuplicates::cMenuDuplicates()
#if defined LIEMIKUUTIO || VDRVERSNUM >= 10721
:cOsdMenu(tr("Duplicate recordings"), 9, 7, 7)
#else
:cOsdMenu(tr("Duplicate recordings"), 9, 7)
#endif
{
#if VDRVERSNUM >= 10728
  SetMenuCategory(mcRecording);
#endif
  Recordings.StateChanged(recordingsState); // just to get the current state
  helpKeys = -1;
  Set();
  Display();
  SetHelpKeys();
}

cMenuDuplicates::~cMenuDuplicates() {
  helpKeys = -1;
}

void cMenuDuplicates::SetHelpKeys(void) {
  cMenuDuplicateItem *ri = (cMenuDuplicateItem *)Get(Current());
  int NewHelpKeys = 0;
  if (ri) {
    NewHelpKeys = 1;
    if (ri->Visibility().Read() == HIDDEN)
      NewHelpKeys = 2;
  }
  if (NewHelpKeys != helpKeys) {
    switch (NewHelpKeys) {
      case 0: SetHelp(NULL); break;
      case 1:
      case 2: SetHelp(trVDR("Button$Play"), trVDR("Setup"), trVDR("Button$Delete"), NewHelpKeys == 1 ? tr("Hide") : tr("Unhide"));
      default: ;
    }
    helpKeys = NewHelpKeys;
  }
}

void cMenuDuplicates::Set(bool Refresh) {
  struct timeval startTime, stopTime;
  gettimeofday(&startTime, NULL);
#ifdef DEBUG_VISIBILITY
  cVisibility::ClearCounters();
  int isDuplicateCount = 0, menuDuplicateItemCount = 0;
#endif
  const char *CurrentRecording = NULL;
  int currentIndex = -1;
  if (Refresh)
    currentIndex = Current();
  else
    CurrentRecording = cReplayControl::LastReplayed();
  cList<cDuplicateRecording> descriptionless;
  cList<cDuplicateRecording> recordings;
  Clear();
  {
    cThreadLock RecordingsLock(&Recordings);
    Recordings.Sort();
    for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording)) {
      cDuplicateRecording *Item = new cDuplicateRecording(recording);
      if (Item->HasDescription())
        recordings.Add(Item);
      else if (dc.hidden || Item->Visibility().Read() != HIDDEN)
        descriptionless.Add(Item);
      }
  }
  for (cDuplicateRecording *recording = recordings.First(); recording; recording = recordings.Next(recording)) {
    if (!recording->Checked()) {
      recording->SetChecked();
      cList<cDuplicateRecording> duplicates;
      duplicates.Add(new cDuplicateRecording(*recording));
      for (cDuplicateRecording *compare = recordings.First(); compare; compare = recordings.Next(compare)) {
        if (!compare->Checked()) {
#ifdef DEBUG_VISIBILITY
          isDuplicateCount++;
#endif
          if (recording->IsDuplicate(compare)) {
            duplicates.Add(new cDuplicateRecording(*compare));
            compare->SetChecked();
          }
        }
      }
      int count = duplicates.Count();
      if (!dc.hidden && count > 1) {
        for (cDuplicateRecording *DuplicateRecording = duplicates.First(); DuplicateRecording; DuplicateRecording = duplicates.Next(DuplicateRecording)) {
          if (DuplicateRecording->Visibility().Read() == HIDDEN)
            count--;
        }
      }
      if (count > 1) {
        Add(SeparatorItem(cString::sprintf(tr("%d duplicate recordings"), duplicates.Count())));
        for (cDuplicateRecording *DuplicateRecording = duplicates.First(); DuplicateRecording; DuplicateRecording = duplicates.Next(DuplicateRecording)) {
          if (dc.hidden || DuplicateRecording->Visibility().Read() != HIDDEN) {
            cMenuDuplicateItem *Item = new cMenuDuplicateItem(DuplicateRecording);
#ifdef DEBUG_VISIBILITY
            menuDuplicateItemCount++;
#endif
            if (*Item->Text()) {
              Add(Item);
              if (CurrentRecording && strcmp(CurrentRecording, Item->FileName()) == 0)
                SetCurrent(Item);
            } else
              delete Item;
          }
        }
      }
    }
  }
  if (descriptionless.Count() > 0)
    Add(SeparatorItem(cString::sprintf(tr("%d recordings without description"), descriptionless.Count())));
  for (cDuplicateRecording *DescriptionlessRecording = descriptionless.First(); DescriptionlessRecording; DescriptionlessRecording = descriptionless.Next(DescriptionlessRecording)) {
    cMenuDuplicateItem *Item = new cMenuDuplicateItem(DescriptionlessRecording);
#ifdef DEBUG_VISIBILITY
    menuDuplicateItemCount++;
#endif
    if (*Item->Text()) {
      Add(Item);
      if (CurrentRecording && strcmp(CurrentRecording, Item->FileName()) == 0)
        SetCurrent(Item);
    } else
      delete Item;
  }
  if (Count() == 0)
    Add(SeparatorItem(cString::sprintf(tr("%d duplicate recordings"), 0)));
  if (Refresh) {
    if (currentIndex >= 0) {
      if(currentIndex >= Count())
        currentIndex = Count() - 1;
      cOsdItem *current = Get(currentIndex);
      while (current) {
        if (current->Selectable()) {
          SetCurrent(current);
          break;
        }
        current = Prev(current);
      }
    }
    Display();
  }
  gettimeofday(&stopTime, NULL);
  double seconds = (((long long)stopTime.tv_sec * 1000000 + stopTime.tv_usec) - ((long long)startTime.tv_sec * 1000000 + startTime.tv_usec)) / 1000000.0;
#ifdef DEBUG_VISIBILITY
  dsyslog("duplicates: Displaying of duplicates took %.2f seconds, is duplicate count %d, get count %d, read count %d, access count %d, duplicate item count %d.",
    seconds, isDuplicateCount, cVisibility::getCount, cVisibility::readCount, cVisibility::accessCount, menuDuplicateItemCount);
#else
  dsyslog("duplicates: Displaying of duplicates took %.2f seconds.", seconds);
#endif
}

cRecording *cMenuDuplicates::GetRecording(cMenuDuplicateItem *Item) {
  cRecording *recording = Recordings.GetByName(Item->FileName());
  if (!recording)
    Skins.Message(mtError, trVDR("Error while accessing recording!"));
  return recording;
}

eOSState cMenuDuplicates::Delete(void) {
  if (HasSubMenu() || Count() == 0)
    return osContinue;
  cMenuDuplicateItem *ri = (cMenuDuplicateItem *)Get(Current());
  if (ri) {
    if (Interface->Confirm(trVDR("Delete recording?"))) {
      cRecordControl *rc = cRecordControls::GetRecordControl(ri->FileName());
      if (rc) {
        if (Interface->Confirm(trVDR("Timer still recording - really delete?"))) {
          cTimer *timer = rc->Timer();
          if (timer) {
            timer->Skip();
            cRecordControls::Process(time(NULL));
            if (timer->IsSingleEvent()) {
              isyslog("deleting timer %s", *timer->ToDescr());
              Timers.Del(timer);
            }
            Timers.SetModified();
          }
        } else
          return osContinue;
      }
      cRecording *recording = GetRecording(ri);
      if (recording) {
        if (cReplayControl::NowReplaying() && strcmp(cReplayControl::NowReplaying(), ri->FileName()) == 0)
          cControl::Shutdown();
        if (recording->Delete()) {
          cReplayControl::ClearLastReplayed(ri->FileName());
          Recordings.DelByName(ri->FileName());
           Recordings.StateChanged(recordingsState); // update state after deletion
           Set(true);
           SetHelpKeys();
        } else
           Skins.Message(mtError, trVDR("Error while deleting recording!"));
      }
    }
  }
  return osContinue;
}

eOSState cMenuDuplicates::Play(void) {
  if (HasSubMenu() || Count() == 0)
    return osContinue;
  cMenuDuplicateItem *ri = (cMenuDuplicateItem *)Get(Current());
  if (ri) {
    cRecording *recording = GetRecording(ri);
    if (recording) {
#if VDRVERSNUM >= 10728
      cDuplicatesReplayControl::SetRecording(recording->FileName());
#else
      cDuplicatesReplayControl::SetRecording(recording->FileName(), recording->Title());
#endif
      cControl::Shutdown();
      cControl::Launch(new cDuplicatesReplayControl);
      return osEnd;
    }
  }
  return osContinue;
}

eOSState cMenuDuplicates::Setup(void) {
  if (HasSubMenu())
    return osContinue;
  cMenuSetupDuplicates *setupMenu = new cMenuSetupDuplicates(this);
  setupMenu->SetTitle(cString::sprintf("%s - %s", tr("Duplicate recordings"), trVDR("Setup")));
  return AddSubMenu(setupMenu);
}

eOSState cMenuDuplicates::Info(void) {
  if (HasSubMenu() || Count() == 0)
    return osContinue;
  cMenuDuplicateItem *ri = (cMenuDuplicateItem *)Get(Current());
  if (ri) {
    cRecording *recording = GetRecording(ri);
    if (recording && recording->Info()->Title())
      return AddSubMenu(new cMenuDuplicate(recording));
  }
  return osContinue;
}

eOSState cMenuDuplicates::ToggleHidden(void) {
  if (HasSubMenu() || Count() == 0)
    return osContinue;
  cMenuDuplicateItem *ri = (cMenuDuplicateItem *)Get(Current());
  if (ri) {
    bool hidden = ri->Visibility().Read() == HIDDEN;
    if (Interface->Confirm(hidden ? tr("Unhide recording?") : tr("Hide recording?"))) {
      if (ri->Visibility().Write(hidden)) {
        if (dc.hidden)
          ri->Visibility().Set(!hidden);
        else
          Set(true);
        SetHelpKeys();
      } else
        Skins.Message(mtError, tr("Error while setting visibility!"));
    }
  }
  return osContinue;
}

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

  if (state == osUnknown) {
    switch (Key) {
      case kPlay:
      case kRed:    return Play();
      case kGreen:  return Setup();
      case kYellow: return Delete();
      case kOk:
      case kInfo:   return Info();
      case kBlue:   return ToggleHidden();
      case kNone:   if (Recordings.StateChanged(recordingsState))
                      Set(true);
                    break;
      default: break;
    }
  }
  if (!HasSubMenu()) {
    if (Key != kNone)
      SetHelpKeys();
  }
  return state;
}

// --- cMenuSetupDuplicates --------------------------------------------------

cMenuSetupDuplicates::cMenuSetupDuplicates(cMenuDuplicates *MenuDuplicates) {
#if VDRVERSNUM >= 10728
  SetMenuCategory(mcSetup);
#endif
  menuDuplicates = MenuDuplicates;
  Add(new cMenuEditBoolItem(tr("Compare title"), &dc.title));
  Add(new cMenuEditBoolItem(tr("Show hidden"), &dc.hidden));
}

void cMenuSetupDuplicates::Store(void) {
  dc.Store();
  if (menuDuplicates != NULL) {
    menuDuplicates->SetCurrent(NULL);
    menuDuplicates->Set();
  }
}

void cMenuSetupDuplicates::SetTitle(const char *Title) {
  cMenuSetupPage::SetTitle(Title);
}