summaryrefslogtreecommitdiff
path: root/menu-commands.c
blob: 66b58f9ba41716f28f161704fb9b85d693b7b2ab (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
/*
 * Image plugin to VDR (C++)
 *
 * (C) 2004-2011 Andreas Brachold    <vdr07 at deltab.de>
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 */


#include <errno.h>
#include "menu-commands.h"
#include <vdr/i18n.h>

cImageMenuCommands::cImageMenuCommands(const char *szTitle, cImageCommands *pCmds, const char *szFileName)
: cOsdMenu(m_szTitle = strdup(szTitle))
, m_pCmds( pCmds )
, m_szFileName(NULL)
, m_bImageChanged(false)
{
    SetHasHotkeys();
    if(szFileName)
        m_szFileName = strdup(szFileName);

    int i = 0;
    cImageCommand *p = NULL;
    for(;NULL != (p = m_pCmds->Get(i));++i)
    {
        Add(new cOsdItem(hk(p->Title())));
    }
    SetHelp(tr("Execute"), NULL, NULL, tr("Back"));
    Display();
}

cImageMenuCommands::~cImageMenuCommands()
{
    if(m_szTitle)
        free(m_szTitle);
    if(m_szFileName)
        free(m_szFileName);
    
    if(m_pCmds)
        delete m_pCmds;
}

eOSState cImageMenuCommands::Execute(void)
{
  errno = 0; // unset error
  char *sz = NULL;
  cImageCommand *p;
  struct stat stFile,stChanged;
  if(0 == stat(m_szFileName, &stFile) 
    && NULL != (p = m_pCmds->Get(Current())))
  {    
     bool bConfirmed = true;
     if (p->Confirm()) {
        asprintf(&sz, "%s?", p->Title());
        bConfirmed = Interface->Confirm(sz);
        free(sz);
        }
     if (bConfirmed) {
        asprintf(&sz, "%s...", p->Title());
        OSD_InfoMsg(sz);
        free(sz);
        const char *szResult = p->Execute(m_szFileName);
        if(0 == stat(m_szFileName, &stChanged))
        {
          m_bImageChanged = stFile.st_mtime != stChanged.st_mtime;
        }
        if (szResult)
           return AddSubMenu(new cImageMenuResult(p->Title(), szResult));
        return osEnd;
    }
    return osContinue;
  }
  const char* szErr = errno?strerror(errno):tr("Operation failed");
  asprintf(&sz, "%s (%s)", szErr,m_szFileName);
  OSD_ErrorMsg(sz);
  free(sz);
  return osContinue;
}

eOSState cImageMenuCommands::ProcessKey(eKeys nKey)
{
  eOSState state = cOsdMenu::ProcessKey(nKey);

  if (state == osUnknown) {
     switch (nKey) {
       case kRed:
       case kOk:  return Execute();
       case kMenu:
       case kBack:
       case kBlue:
                  return osBack;
       default:   break;
       }
     }
  return state;
}



cImageMenuResult::cImageMenuResult(const char *szTitle, const char *szText)
: cOsdMenu(szTitle)
, m_szText(szText)
{
  SetHelp(NULL, NULL, NULL, tr("Back"));
}


eOSState cImageMenuResult::ProcessKey(eKeys nKey)
{
  switch (nKey & ~k_Repeat) {
    case kUp:
    case kDown:
    case kLeft:
    case kRight:
                  DisplayMenu()->Scroll(NORMALKEY(nKey) == kUp || NORMALKEY(nKey) == kLeft, NORMALKEY(nKey) == kLeft || NORMALKEY(nKey) == kRight);
                  return osContinue;
    default: break;
    }

  eOSState nState = cOsdMenu::ProcessKey(nKey);
  if (nState == osUnknown) {
     switch (nKey) {
       case kOk: 
       case kMenu:
       case kBack:
       case kBlue:
                 return osBack;
       default:  nState = osContinue;
       }
     }
  return nState;
}

void cImageMenuResult::Display(void)
{
  cOsdMenu::Display();
  DisplayMenu()->SetText(m_szText, true);
}