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  auto *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  foreach (auto a, *m_archiveList)
212  {
213  bool found = false;
214 
215  for (int y = 0; y < m_selectedList.size(); y++)
216  {
217  f = m_selectedList.at(y);
218  if (a->type != "File" || a->filename == f)
219  {
220  found = true;
221  break;
222  }
223  }
224 
225  if (!found)
226  tempAList.append(a);
227  }
228 
229  foreach (auto x, tempAList)
230  m_archiveList->removeAll(x);
231 
232  // remove any items that are already in the list
233  QStringList tempSList;
234  for (int x = 0; x < m_selectedList.size(); x++)
235  {
236  f = m_selectedList.at(x);
237 
238  foreach (auto a, *m_archiveList)
239  {
240  if (a->filename == f)
241  {
242  tempSList.append(f);
243  break;
244  }
245  }
246  }
247 
248  for (int x = 0; x < tempSList.size(); x++)
249  m_selectedList.removeAll(tempSList.at(x));
250 
251  // add all that are left
252  for (int x = 0; x < m_selectedList.size(); x++)
253  {
254  f = m_selectedList.at(x);
255 
256  QFile file(f);
257  if (file.exists())
258  {
259  QString title = f;
260  int pos = f.lastIndexOf('/');
261  if (pos > 0)
262  title = f.mid(pos + 1);
263 
264  auto *a = new ArchiveItem;
265  a->type = "File";
266  a->title = title;
267  a->subtitle = "";
268  a->description = "";
269  a->startDate = "";
270  a->startTime = "";
271  a->size = (int64_t)file.size();
272  a->filename = f;
273  a->hasCutlist = false;
274  a->useCutlist = false;
275  a->duration = 0;
276  a->cutDuration = 0;
277  a->videoWidth = 0;
278  a->videoHeight = 0;
279  a->fileCodec = "";
280  a->videoCodec = "";
281  a->encoderProfile = nullptr;
282  a->editedDetails = false;
283  m_archiveList->append(a);
284  }
285  }
286  }
287  else
288  {
290  auto *fileData = item->GetData().value<FileData*>();
291 
293  {
294  if (!fileData->directory)
295  {
296  ShowOkPopup(tr("The selected item is not a directory!"));
297  return;
298  }
299 
300  if (fileData->filename != "..")
301  {
302  if (!m_curDirectory.endsWith("/"))
303  m_curDirectory += "/";
304  m_curDirectory += fileData->filename;
305  }
306  }
307  else
308  {
309  if (fileData->directory)
310  {
311  if (!m_curDirectory.endsWith("/"))
312  m_curDirectory += "/";
313  }
314  else
315  {
316  if (!m_curDirectory.endsWith("/"))
317  m_curDirectory += "/";
318  m_curDirectory += fileData->filename;
319  }
320  }
321  }
322 
324  emit haveResult(true);
325  else
326  emit haveResult(getSelected());
327  Close();
328 }
329 
331 {
333  emit haveResult(true);
334  else
335  emit haveResult(QString());
336  Close();
337 }
338 
340 {
341  if (!m_archiveList)
342  return;
343 
344  while (!m_selectedList.isEmpty())
345  m_selectedList.takeFirst();
346  m_selectedList.clear();
347 
348  foreach (auto a, *m_archiveList)
349  {
350  foreach (auto f, m_fileData)
351  {
352  if (f->filename == a->filename)
353  {
354  if (m_selectedList.indexOf(f->filename) == -1)
355  m_selectedList.append(f->filename);
356  break;
357  }
358  }
359  }
360 }
361 
363 {
364  if (!m_fileButtonList)
365  return;
366 
368  while (!m_fileData.isEmpty())
369  delete m_fileData.takeFirst();
370  m_fileData.clear();
371 
372  QDir d;
373 
374  d.setPath(m_curDirectory);
375  if (d.exists())
376  {
377  // first get a list of directory's in the current directory
378  QStringList filters;
379  filters << "*";
380  QFileInfoList list = d.entryInfoList(filters, QDir::Dirs, QDir::Name);
381 
382  foreach (const auto & fi, list)
383  {
384  if (fi.fileName() != ".")
385  {
386  auto *data = new FileData;
387  data->selected = false;
388  data->directory = true;
389  data->filename = fi.fileName();
390  data->size = 0;
391  m_fileData.append(data);
392 
393  // add a row to the MythUIButtonList
394  auto* item = new
395  MythUIButtonListItem(m_fileButtonList, data->filename);
396  item->setCheckable(false);
397  item->SetImage("ma_folder.png");
398  item->SetData(QVariant::fromValue(data));
399  }
400  }
401 
403  {
404  // second get a list of file's in the current directory
405  filters.clear();
406  filters = m_filemask.split(" ", QString::SkipEmptyParts);
407  list = d.entryInfoList(filters, QDir::Files, QDir::Name);
408  foreach (const auto & fi, list)
409  {
410  auto *data = new FileData;
411  data->selected = false;
412  data->directory = false;
413  data->filename = fi.fileName();
414  data->size = fi.size();
415  m_fileData.append(data);
416 
417  // add a row to the UIListBtnArea
418  auto* item =
419  new MythUIButtonListItem(m_fileButtonList, data->filename);
420  item->SetText(formatSize(data->size / 1024, 2), "size");
421 
423  {
424  item->setCheckable(true);
425 
426  QString fullPath = m_curDirectory;
427  if (!fullPath.endsWith("/"))
428  fullPath += "/";
429  fullPath += data->filename;
430 
431  if (m_selectedList.indexOf(fullPath) != -1)
432  {
433  item->setChecked(MythUIButtonListItem::FullChecked);
434  }
435  else
436  {
437  item->setChecked(MythUIButtonListItem::NotChecked);
438  }
439  }
440  else
441  item->setCheckable(false);
442 
443  item->SetData(QVariant::fromValue(data));
444  }
445  }
447  }
448  else
449  {
450  m_locationEdit->SetText("/");
451  LOG(VB_GENERAL, LOG_ERR,
452  "MythArchive: current directory does not exist!");
453  }
454 }
QList< FileData * > m_fileData
Definition: fileselector.h:80
~FileSelector() override
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:86
void cancelPressed(void)
MythUIButton * m_okButton
Definition: fileselector.h:89
bool selected
Definition: fileselector.h:21
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
void backPressed(void)
void updateFileList(void)
QString m_curDirectory
Definition: fileselector.h:79
bool Create(void) override
void BuildFocusList(void)
MythUIButton * m_homeButton
Definition: fileselector.h:92
virtual void Close()
void updateSelectedList(void)
MythUIButton * m_cancelButton
Definition: fileselector.h:90
static const uint16_t * d
MythUIButtonList * m_fileButtonList
Definition: fileselector.h:87
FSTYPE m_selectorType
Definition: fileselector.h:77
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
void homePressed(void)
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)
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
static QString formatSize(int64_t sizeKB, int prec)
QStringList m_selectedList
Definition: fileselector.h:81
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
bool keyPressEvent(QKeyEvent *e) override
Key event handler.
QList< ArchiveItem * > * m_archiveList
Definition: fileselector.h:82
MythUIButton * m_backButton
Definition: fileselector.h:91
void OKPressed(void)
QString getSelected(void)
bool SetFocusWidget(MythUIType *widget=nullptr)
void itemClicked(MythUIButtonListItem *item)
void haveResult(bool ok)
void setChecked(CheckState state)
void locationEditLostFocus(void)
QString m_filemask
Definition: fileselector.h:78
QString GetText(void) const
MythUIButtonListItem * GetItemCurrent() const
MythUITextEdit * m_locationEdit
Definition: fileselector.h:88