MythTV  master
fileselector.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 
3 // Qt
4 #include <QApplication>
5 #include <QDir>
6 #include <QFileInfo>
7 #include <QVariant>
8 
9 // Myth
10 #include <mythcontext.h>
11 #include <mythdb.h>
12 #include <mythuihelper.h>
13 #include <mythuitext.h>
14 #include <mythuitextedit.h>
15 #include <mythuibutton.h>
16 #include <mythuiimage.h>
17 #include <mythuibuttonlist.h>
18 #include <mythmainwindow.h>
19 #include <mythdialogbox.h>
20 
21 // mytharchive
22 #include "fileselector.h"
23 #include "archiveutil.h"
24 
26 
28 {
29  while (!m_fileData.isEmpty())
30  delete m_fileData.takeFirst();
31 }
32 
34 {
35  // Load the theme for this screen
36  bool foundtheme = LoadWindowFromXML("mytharchive-ui.xml", "file_selector", this);
37  if (!foundtheme)
38  return false;
39 
40  bool err = false;
41  UIUtilW::Assign(this, m_titleText, "title_text");
42  UIUtilE::Assign(this, m_fileButtonList, "filelist", &err);
43  UIUtilE::Assign(this, m_locationEdit, "location_edit", &err);
44  UIUtilE::Assign(this, m_backButton, "back_button", &err);
45  UIUtilE::Assign(this, m_homeButton, "home_button", &err);
46  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
47  UIUtilE::Assign(this, m_cancelButton, "cancel_button", &err);
48 
49  if (err)
50  {
51  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'file_selector'");
52  return false;
53  }
54 
55  if (m_titleText)
56  {
57  switch (m_selectorType)
58  {
59  case FSTYPE_FILE:
60  m_titleText->SetText(tr("Find File"));
61  break;
62  case FSTYPE_DIRECTORY:
63  m_titleText->SetText(tr("Find Directory"));
64  break;
65  default:
66  m_titleText->SetText(tr("Find Files"));
67  break;
68  }
69  }
70 
71  connect(m_okButton, SIGNAL(Clicked()), this, SLOT(OKPressed()));
72  connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(cancelPressed()));
73 
74  connect(m_locationEdit, SIGNAL(LosingFocus()),
75  this, SLOT(locationEditLostFocus()));
77 
78  connect(m_backButton, SIGNAL(Clicked()), this, SLOT(backPressed()));
79  connect(m_homeButton, SIGNAL(Clicked()), this, SLOT(homePressed()));
80 
82  this, SLOT(itemClicked(MythUIButtonListItem *)));
83 
85 
87 
90 
91  return true;
92 }
93 
94 bool FileSelector::keyPressEvent(QKeyEvent *event)
95 {
96  if (GetFocusWidget()->keyPressEvent(event))
97  return true;
98 
99  QStringList actions;
100  bool handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
101 
102  for (int i = 0; i < actions.size() && !handled; i++)
103  {
104  QString action = actions[i];
105  handled = true;
106 
107  if (action == "MENU")
108  {
109 
110  }
111  else
112  handled = false;
113  }
114 
115  if (!handled && MythScreenType::keyPressEvent(event))
116  handled = true;
117 
118  return handled;
119 }
120 
122 {
123  if (!item)
124  return;
125 
126  FileData *fileData = item->GetData().value<FileData*>();
127 
128  if (fileData->directory)
129  {
130  if (fileData->filename == "..")
131  {
132  // move up on directory
133  int pos = m_curDirectory.lastIndexOf('/');
134  if (pos > 0)
135  m_curDirectory = m_curDirectory.left(pos);
136  else
137  m_curDirectory = "/";
138  }
139  else
140  {
141  if (!m_curDirectory.endsWith("/"))
142  m_curDirectory += "/";
143  m_curDirectory += fileData->filename;
144  }
145  updateFileList();
146  }
147  else
148  {
150  {
151  QString fullPath = m_curDirectory;
152  if (!fullPath.endsWith("/"))
153  fullPath += "/";
154  fullPath += fileData->filename;
155 
157  {
158  m_selectedList.removeAll(fullPath);
160  }
161  else
162  {
163  if (m_selectedList.indexOf(fullPath) == -1)
164  m_selectedList.append(fullPath);
166  }
167  }
168  }
169 }
170 
172 {
173  return m_curDirectory;
174 }
175 
177 {
179  updateFileList();
180 }
181 
183 {
184  // move up one directory
185  int pos = m_curDirectory.lastIndexOf('/');
186  if (pos > 0)
187  m_curDirectory = m_curDirectory.left(pos);
188  else
189  m_curDirectory = "/";
190 
191  updateFileList();
192 }
193 
195 {
196  char *home = getenv("HOME");
197  m_curDirectory = home;
198 
199  updateFileList();
200 }
201 
203 {
205  {
206  // loop though selected files and add them to the list
207  QString f;
208 
209  // remove any items that have been removed from the list
210  QList<ArchiveItem *> tempAList;
211  for (int x = 0; x < m_archiveList->size(); x++)
212  {
213  ArchiveItem *a = m_archiveList->at(x);
214  bool found = false;
215 
216  for (int y = 0; y < m_selectedList.size(); y++)
217  {
218  f = m_selectedList.at(y);
219  if (a->type != "File" || a->filename == f)
220  {
221  found = true;
222  break;
223  }
224  }
225 
226  if (!found)
227  tempAList.append(a);
228  }
229 
230  for (int x = 0; x < tempAList.size(); x++)
231  m_archiveList->removeAll(tempAList.at(x));
232 
233  // remove any items that are already in the list
234  QStringList tempSList;
235  for (int x = 0; x < m_selectedList.size(); x++)
236  {
237  f = m_selectedList.at(x);
238 
239  for (int y = 0; y < m_archiveList->size(); y++)
240  {
241  ArchiveItem *a = m_archiveList->at(y);
242  if (a->filename == f)
243  {
244  tempSList.append(f);
245  break;
246  }
247  }
248  }
249 
250  for (int x = 0; x < tempSList.size(); x++)
251  m_selectedList.removeAll(tempSList.at(x));
252 
253  // add all that are left
254  for (int x = 0; x < m_selectedList.size(); x++)
255  {
256  f = m_selectedList.at(x);
257 
258  QFile file(f);
259  if (file.exists())
260  {
261  QString title = f;
262  int pos = f.lastIndexOf('/');
263  if (pos > 0)
264  title = f.mid(pos + 1);
265 
266  ArchiveItem *a = new ArchiveItem;
267  a->type = "File";
268  a->title = title;
269  a->subtitle = "";
270  a->description = "";
271  a->startDate = "";
272  a->startTime = "";
273  a->size = (int64_t)file.size();
274  a->filename = f;
275  a->hasCutlist = false;
276  a->useCutlist = false;
277  a->duration = 0;
278  a->cutDuration = 0;
279  a->videoWidth = 0;
280  a->videoHeight = 0;
281  a->fileCodec = "";
282  a->videoCodec = "";
283  a->encoderProfile = nullptr;
284  a->editedDetails = false;
285  m_archiveList->append(a);
286  }
287  }
288  }
289  else
290  {
292  FileData *fileData = item->GetData().value<FileData*>();
293 
295  {
296  if (!fileData->directory)
297  {
298  ShowOkPopup(tr("The selected item is not a directory!"));
299  return;
300  }
301 
302  if (fileData->filename != "..")
303  {
304  if (!m_curDirectory.endsWith("/"))
305  m_curDirectory += "/";
306  m_curDirectory += fileData->filename;
307  }
308  }
309  else
310  {
311  if (fileData->directory)
312  {
313  if (!m_curDirectory.endsWith("/"))
314  m_curDirectory += "/";
315  }
316  else
317  {
318  if (!m_curDirectory.endsWith("/"))
319  m_curDirectory += "/";
320  m_curDirectory += fileData->filename;
321  }
322  }
323  }
324 
326  emit haveResult(true);
327  else
328  emit haveResult(getSelected());
329  Close();
330 }
331 
333 {
335  emit haveResult(true);
336  else
337  emit haveResult(QString());
338  Close();
339 }
340 
342 {
343  if (!m_archiveList)
344  return;
345 
346  while (!m_selectedList.isEmpty())
347  m_selectedList.takeFirst();
348  m_selectedList.clear();
349 
350  for (int x = 0; x < m_archiveList->size(); x++)
351  {
352  ArchiveItem *a = m_archiveList->at(x);
353  for (int y = 0; y < m_fileData.size(); y++)
354  {
355  FileData *f = m_fileData.at(y);
356  if (f->filename == a->filename)
357  {
358  if (m_selectedList.indexOf(f->filename) == -1)
359  m_selectedList.append(f->filename);
360  break;
361  }
362  }
363  }
364 }
365 
367 {
368  if (!m_fileButtonList)
369  return;
370 
372  while (!m_fileData.isEmpty())
373  delete m_fileData.takeFirst();
374  m_fileData.clear();
375 
376  QDir d;
377 
378  d.setPath(m_curDirectory);
379  if (d.exists())
380  {
381  // first get a list of directory's in the current directory
382  QStringList filters;
383  filters << "*";
384  QFileInfoList list = d.entryInfoList(filters, QDir::Dirs, QDir::Name);
385  QFileInfo fi;
386 
387  for (int x = 0; x < list.size(); x++)
388  {
389  fi = list.at(x);
390  if (fi.fileName() != ".")
391  {
392  FileData *data = new FileData;
393  data->selected = false;
394  data->directory = true;
395  data->filename = fi.fileName();
396  data->size = 0;
397  m_fileData.append(data);
398 
399  // add a row to the MythUIButtonList
400  MythUIButtonListItem* item = new
402  item->setCheckable(false);
403  item->SetImage("ma_folder.png");
404  item->SetData(qVariantFromValue(data));
405  }
406  }
407 
409  {
410  // second get a list of file's in the current directory
411  filters.clear();
412  filters = m_filemask.split(" ", QString::SkipEmptyParts);
413  list = d.entryInfoList(filters, QDir::Files, QDir::Name);
414  for (int x = 0; x < list.size(); x++)
415  {
416  fi = list.at(x);
417  FileData *data = new FileData;
418  data->selected = false;
419  data->directory = false;
420  data->filename = fi.fileName();
421  data->size = fi.size();
422  m_fileData.append(data);
423 
424  // add a row to the UIListBtnArea
425  MythUIButtonListItem* item =
427  item->SetText(formatSize(data->size / 1024, 2), "size");
428 
430  {
431  item->setCheckable(true);
432 
433  QString fullPath = m_curDirectory;
434  if (!fullPath.endsWith("/"))
435  fullPath += "/";
436  fullPath += data->filename;
437 
438  if (m_selectedList.indexOf(fullPath) != -1)
439  {
441  }
442  else
443  {
445  }
446  }
447  else
448  item->setCheckable(false);
449 
450  item->SetData(qVariantFromValue(data));
451  }
452  }
454  }
455  else
456  {
457  m_locationEdit->SetText("/");
458  LOG(VB_GENERAL, LOG_ERR,
459  "MythArchive: current directory does not exist!");
460  }
461 }
QList< FileData * > m_fileData
Definition: fileselector.h:79
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
void LosingFocus()
MythConfirmationDialog * ShowOkPopup(const QString &message, QObject *parent, const char *slot, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
MythUIText * m_titleText
Definition: fileselector.h:85
void cancelPressed(void)
void SetData(QVariant data)
MythUIButton * m_okButton
Definition: fileselector.h:88
bool hasCutlist
Definition: archiveutil.h:69
bool selected
Definition: fileselector.h:20
bool useCutlist
Definition: archiveutil.h:70
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
void backPressed(void)
int videoWidth
Definition: archiveutil.h:68
void updateFileList(void)
QString m_curDirectory
Definition: fileselector.h:78
bool Create(void) override
void BuildFocusList(void)
MythUIButton * m_homeButton
Definition: fileselector.h:91
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache.
QString startTime
Definition: archiveutil.h:59
QString filename
Definition: archiveutil.h:60
virtual void Close()
void setCheckable(bool flag)
void updateSelectedList(void)
MythUIButton * m_cancelButton
Definition: fileselector.h:89
static const uint16_t * d
MythUIButtonList * m_fileButtonList
Definition: fileselector.h:86
QString fileCodec
Definition: archiveutil.h:66
bool editedDetails
Definition: archiveutil.h:71
int cutDuration
Definition: archiveutil.h:64
FSTYPE m_selectorType
Definition: fileselector.h:76
int64_t size
Definition: fileselector.h:22
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
void SetText(const QString &text, const QString &name="", const QString &state="")
void homePressed(void)
bool directory
Definition: fileselector.h:19
CheckState state() const
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
void SetText(const QString &text, bool moveCursor=true)
MythUIType * GetFocusWidget(void) const
QString type
Definition: archiveutil.h:54
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
MythMainWindow * GetMythMainWindow(void)
QString filename
Definition: fileselector.h:21
QString subtitle
Definition: archiveutil.h:56
static QString formatSize(int64_t sizeKB, int prec)
QString videoCodec
Definition: archiveutil.h:67
QStringList m_selectedList
Definition: fileselector.h:80
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString startDate
Definition: archiveutil.h:58
QString title
Definition: archiveutil.h:55
bool keyPressEvent(QKeyEvent *e) override
Key event handler.
QList< ArchiveItem * > * m_archiveList
Definition: fileselector.h:81
MythUIButton * m_backButton
Definition: fileselector.h:90
EncoderProfile * encoderProfile
Definition: archiveutil.h:65
void OKPressed(void)
int videoHeight
Definition: archiveutil.h:68
QString description
Definition: archiveutil.h:57
QString getSelected(void)
bool SetFocusWidget(MythUIType *widget=nullptr)
int64_t size
Definition: archiveutil.h:61
void itemClicked(MythUIButtonListItem *item)
void haveResult(bool ok)
void setChecked(CheckState state)
void locationEditLostFocus(void)
QString m_filemask
Definition: fileselector.h:77
QString GetText(void) const
MythUIButtonListItem * GetItemCurrent() const
MythUITextEdit * m_locationEdit
Definition: fileselector.h:87