summaryrefslogtreecommitdiff
path: root/commands.h
blob: 3457b3b1072d8cd58155ff8023aec046f26f5958 (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
#ifndef __COMMANDS_DVDSWITCH_H
#define __COMMANDS_DVDSWITCH_H

#include <vdr/plugin.h>
#include "imagelist.h"
#include "menu.h"
#include "setup.h"
#include <stdlib.h>

enum eCMDs
{
  cmdNone,
  cmdDirManage,
  cmdDVDopen,
  cmdDVDclose,
  cmdImgPlay,
  cmdImgRename,
  cmdImgMove,
  cmdImgDelete,
  cmdImgBurn,
  cmdImgRead,
  cmdCommands
};

enum eCMDState
{
  csNone,
  csDirNew,
  csDirEdit,
  csDirMove,
  csDirDel
};


class cCMD
{
  public:
    static eOSState Play(cMainMenuItem *item = NULL);
    static eOSState Eject(bool close = false);
};


class cCMDMenu : public cOsdMenu
{
  private:
    cMainMenuItem *iItem;
    cMainMenu *OsdObject;
    cImageList &ImageList;
  public:
    cCMDMenu(cImageList &ImageList, cMainMenuItem *item, cMainMenu *osdobject);
    virtual eOSState ProcessKey(eKeys Key);
};

class cCMDDir : public cOsdMenu, private cDirHandlingOpt
{
  private:
    eCMDState State;
    char Dir[MaxFileName];
    cMainMenu *OsdObject;
    bool Select;
    char *Buffer;
    cImageList &ImageList;

    void Build(char *dir = NULL);
    void SetDir(char *value = NULL)
    {
      if(value)
        strn0cpy(Dir, value, MaxFileName);
      else
        strcpy(Dir, "\0");
    }

    eOSState New(void);
    eOSState New(eKeys Key);
    eOSState Edit(cMainMenuItem *mItem);
    eOSState Edit(eKeys Key);
  public:
    cCMDDir(cImageList &ImageList, cMainMenu *osdobject = NULL, bool select = false, char *buffer = NULL);

    virtual eOSState ProcessKey(eKeys Key);
    void SetHelp(void);
};

class cCMDMove : public cOsdMenu, private cDirHandlingOpt
{
  private:
    char *File;
    cMainMenu *OsdObject;
    bool Dir;
    bool Direct;
    cImageList &ImageList;

    void Build(char *dir = NULL);
  public:
    cCMDMove(cImageList &ImageList, const char *file, cMainMenu *osdobject, bool dir = true, bool direct = false);
    ~cCMDMove(void) { free(File); }

    virtual eOSState ProcessKey(eKeys Key);
    void SetHelp(void);
};

class cCMDImage
{
  private:
    char *File;
    cMainMenu *OsdObject;
  public:
    char NewFile[MaxFileName];
    int tmpHideTypeCol;
    //int tmpHideImgSize

    cCMDImage(cMainMenu *osdobject = NULL);
    ~cCMDImage(void);

    char* Rename(const char *file = NULL);
    eOSState Delete(const char *file = NULL);
    eOSState Burn(const char *file);
};

class cCMDImageRead : public cOsdMenu
{
  private:
    char File[MaxFileName];
    char Dir[MaxFileName];
    char ImgTypeTxt[MaxFileName];
    int ImgType;
    cImageList &ImageList;

  public:
    cCMDImageRead(cImageList &ImageList);
    ~cCMDImageRead(void);

    void SetHelp(void);
    virtual eOSState ProcessKey(eKeys Key);
};

class cCMDImageReadThread : public cThread
{
  private:
    char *File;
    char *Dir;
    eFileInfo FileType;
  protected:
    virtual void Action(void)
    {
      dsyslog("ReadThread started");
      if(File && Dir && FileType != tNone)
      {
        dsyslog("ReadThread executed");
        char *cmd = NULL;
        char *output = NULL;
        char *mountpoint = NULL;
        int step = 0;
        cTokenizer *token = NULL;
        char buffer[MaxFileName];
        if(realpath(DVDSwitchSetup.DVDLinkOrg, buffer)) {
          while(!mountpoint && step < 2) {
            step++;
            if(0 < asprintf(&cmd,
                     "cat /etc/fstab | grep -e \"%s\" | grep -e \"^[^#]\"",
                     step == 1 ? DVDSwitchSetup.DVDLinkOrg : buffer)) {
              dsyslog("ReadThread call: %s", cmd);

              FILE *p = popen(cmd, "r");
              if(p)
              {
#if VDRVERSNUM >= 10318
                cReadLine rl;
                output = rl.Read(p);
#else
                output = readline(p);
#endif
                pclose(p);
              }
              token = new cTokenizer(output, " ");
              if(token->Count() > 1) {
                char* s = strdup(token->GetToken(2));
                mountpoint = stripspace(s);
              }
              DELETENULL(token);
            }
            FREENULL(cmd);
          }
          if(0 < asprintf(&cmd,
                    "'%s' '%s' '%s' '%s' '%s' '%s'",
                    DVDSwitchSetup.DVDReadScript,
                    Dir,
                    File,
                    buffer,
                    mountpoint,
                    (FileType == tFile) ? "IMAGE" : "DIR")) {
            dsyslog("ReadThread call: %s", cmd);
            int rc = system(cmd);
            dsyslog("ReadThread return value: %i", rc);
            FREENULL(cmd);
          }
          FREENULL(mountpoint);
        }
      }
      delete(this);
    };
  public:
    cCMDImageReadThread(char *file, char *dir, int imgtype, cImageList &ImageList)
    {
      dsyslog("ReadThread created");
      File = NULL;
      Dir = NULL;
      FileType = tNone;

      cImageListItem *item = ImageList.Get(imgtype);
      if(imgtype >= 0 && file && dir)
      {
        if(item->GetFType() == tFile) {
          if(0 >= asprintf(&File, "%s%s", file, item->GetValue()))
            return;
        }
        else
          File = strdup(file);
        Dir = strdup(dir);
        FileType = item->GetFType();
      }
    }
    ~cCMDImageReadThread(void)
    {
      free(File);
      free(Dir);
    }
};

class cCMDImageBurnThread : public cThread
{
  private:
    char *File;
    eFileInfo FileType;
  protected:
    virtual void Action(void)
    {
      dsyslog("BurnThread started");
      if(File && FileType != tNone)
      {
        dsyslog("BurnThread executed");
        char *cmd;
        if(0 < asprintf(&cmd,
                  "'%s' '%s' '%s'",
                  DVDSwitchSetup.DVDWriteScript,
                  File,
                  FileType == tFile ? "IMAGE" : "DIR")) {
          dsyslog("BurnThread call: %s", cmd);
          int rc = system(cmd);
          dsyslog("BurnThread return value: %i", rc);
          FREENULL(cmd);
        }
      }
      delete(this);
    };
  public:
    cCMDImageBurnThread(const char *file, eFileInfo type)
    {
      dsyslog("BurnThread created");
      File = NULL;
      FileType = tNone;

      if(file && type != tNone)
      {
        File = strdup(file);
        FileType = type;
      }
    }
    ~cCMDImageBurnThread(void) { free(File); }
};

#endif // __COMMANDS_DVDSWITCH_H