00001 #include <cstdlib>
00002
00003 #include <qdir.h>
00004 #include <qapplication.h>
00005 #include <qfileinfo.h>
00006
00007
00008 #include <mythtv/mythcontext.h>
00009 #include <mythtv/mythdbcon.h>
00010 #include <mythtv/uitypes.h>
00011
00012
00013 #include "directoryfinder.h"
00014
00016
00017 DirectoryFinder::DirectoryFinder(const QString &startDir,
00018 MythMainWindow *parent,
00019 const char *name)
00020 :MythThemedDialog(parent, "directory_finder", "music-", name)
00021 {
00022 m_curDirectory = startDir;
00023 wireUpTheme();
00024 }
00025
00026 DirectoryFinder::~DirectoryFinder()
00027 {
00028 }
00029
00030 QString DirectoryFinder::getSelected(void)
00031 {
00032 return m_curDirectory;
00033 }
00034
00035 void DirectoryFinder::keyPressEvent(QKeyEvent *e)
00036 {
00037 bool handled = false;
00038
00039 QStringList actions;
00040 gContext->GetMainWindow()->TranslateKeyPress("Global", e, actions);
00041
00042 for (unsigned int i = 0; i < actions.size() && !handled; i++)
00043 {
00044 QString action = actions[i];
00045 handled = true;
00046
00047 if (action == "SELECT")
00048 {
00049 if (getCurrentFocusWidget() == m_fileList)
00050 {
00051 UIListBtnTypeItem *item = m_fileList->GetItemCurrent();
00052 int index = (intptr_t) item->getData();
00053 if (m_directoryList[index] == "..")
00054 {
00055
00056 int pos = m_curDirectory.findRev('/');
00057 if (pos > 0)
00058 m_curDirectory = m_curDirectory.left(pos);
00059 else
00060 m_curDirectory = "/";
00061 }
00062 else
00063 {
00064 if (!m_curDirectory.endsWith("/"))
00065 m_curDirectory += "/";
00066 m_curDirectory += m_directoryList[index];
00067 }
00068
00069 updateFileList();
00070 }
00071 else
00072 activateCurrent();
00073 }
00074 else if (action == "UP")
00075 {
00076 if (getCurrentFocusWidget() == m_fileList)
00077 {
00078 m_fileList->MoveUp(UIListBtnType::MoveItem);
00079 m_fileList->refresh();
00080 }
00081 else
00082 nextPrevWidgetFocus(false);
00083 }
00084 else if (action == "DOWN")
00085 {
00086 if (getCurrentFocusWidget() == m_fileList)
00087 {
00088 m_fileList->MoveDown(UIListBtnType::MoveItem);
00089 m_fileList->refresh();
00090 }
00091 else
00092 nextPrevWidgetFocus(true);
00093 }
00094 else if (action == "LEFT")
00095 {
00096 nextPrevWidgetFocus(false);
00097 }
00098 else if (action == "RIGHT")
00099 {
00100 nextPrevWidgetFocus(true);
00101 }
00102 else if (action == "PAGEUP")
00103 {
00104 if (getCurrentFocusWidget() == m_fileList)
00105 {
00106 m_fileList->MoveUp(UIListBtnType::MovePage);
00107 m_fileList->refresh();
00108 }
00109 }
00110 else if (action == "PAGEDOWN")
00111 {
00112 if (getCurrentFocusWidget() == m_fileList)
00113 {
00114 m_fileList->MoveDown(UIListBtnType::MovePage);
00115 m_fileList->refresh();
00116 }
00117 }
00118 else
00119 handled = false;
00120 }
00121
00122 if (!handled)
00123 MythThemedDialog::keyPressEvent(e);
00124 }
00125
00126 void DirectoryFinder::wireUpTheme()
00127 {
00128 m_fileList = getUIListBtnType("filelist");
00129
00130 m_locationEdit = getUIRemoteEditType("location_edit");
00131 if (m_locationEdit)
00132 {
00133 m_locationEdit->createEdit(this);
00134 connect(m_locationEdit, SIGNAL(loosingFocus()),
00135 this, SLOT(locationEditLostFocus()));
00136 }
00137
00138
00139 m_okButton = getUITextButtonType("ok_button");
00140 if (m_okButton)
00141 {
00142 m_okButton->setText(tr("OK"));
00143 connect(m_okButton, SIGNAL(pushed()), this, SLOT(OKPressed()));
00144 }
00145
00146
00147 m_cancelButton = getUITextButtonType("cancel_button");
00148 if (m_cancelButton)
00149 {
00150 m_cancelButton->setText(tr("Cancel"));
00151 connect(m_cancelButton, SIGNAL(pushed()), this, SLOT(cancelPressed()));
00152 }
00153
00154
00155 m_backButton = getUITextButtonType("back_button");
00156 if (m_backButton)
00157 {
00158 m_backButton->setText(tr("Back"));
00159 connect(m_backButton, SIGNAL(pushed()), this, SLOT(backPressed()));
00160 }
00161
00162
00163 m_homeButton = getUITextButtonType("home_button");
00164 if (m_homeButton)
00165 {
00166 m_homeButton->setText(tr("Home"));
00167 connect(m_homeButton, SIGNAL(pushed()), this, SLOT(homePressed()));
00168 }
00169
00170 if (!m_fileList || !m_locationEdit || !m_backButton || !m_okButton
00171 || !m_cancelButton || !m_homeButton)
00172 {
00173 cout << "DirectoryFinder: Your theme is missing some UI elements! Bailing out." << endl;
00174 QTimer::singleShot(100, this, SLOT(reject()));
00175 }
00176
00177
00178 m_directoryPixmap = gContext->LoadScalePixmap("mm_folder.png");
00179
00180 buildFocusList();
00181 assignFirstFocus();
00182 updateFileList();
00183 }
00184
00185 void DirectoryFinder::locationEditLostFocus()
00186 {
00187 m_curDirectory = m_locationEdit->getText();
00188 updateFileList();
00189 }
00190
00191 void DirectoryFinder::backPressed()
00192 {
00193
00194 int pos = m_curDirectory.findRev('/');
00195 if (pos > 0)
00196 m_curDirectory = m_curDirectory.left(pos);
00197 else
00198 m_curDirectory = "/";
00199
00200 updateFileList();
00201 }
00202
00203 void DirectoryFinder::homePressed()
00204 {
00205 char *home = getenv("HOME");
00206 m_curDirectory = home;
00207
00208 updateFileList();
00209 }
00210
00211 void DirectoryFinder::OKPressed()
00212 {
00213 UIListBtnTypeItem *item = m_fileList->GetItemCurrent();
00214 int index = (intptr_t) item->getData();
00215
00216 if (m_directoryList[index] != "..")
00217 {
00218 if (!m_curDirectory.endsWith("/"))
00219 m_curDirectory += "/";
00220 m_curDirectory += m_directoryList[index];
00221 }
00222
00223 done(Accepted);
00224 }
00225
00226 void DirectoryFinder::cancelPressed()
00227 {
00228 reject();
00229 }
00230
00231 void DirectoryFinder::updateFileList()
00232 {
00233 if (!m_fileList)
00234 return;
00235
00236 m_fileList->Reset();
00237 m_directoryList.clear();
00238
00239 QDir d;
00240
00241 d.setPath(m_curDirectory);
00242
00243 if (!d.exists())
00244 {
00245 cout << "DirectoryFinder: current directory does not exist!" << endl;
00246 m_locationEdit->setText("/");
00247 m_curDirectory = "/";
00248 d.setPath("/");
00249 }
00250
00251 const QFileInfoList *list = d.entryInfoList("*", QDir::Dirs, QDir::Name);
00252 if (!list)
00253 {
00254 m_directoryList.append("..");
00255
00256
00257 UIListBtnTypeItem* item = new UIListBtnTypeItem(
00258 m_fileList, "..");
00259 item->setCheckable(false);
00260 item->setPixmap(m_directoryPixmap);
00261 item->setData((void*) 0);
00262 }
00263 else
00264 {
00265 QFileInfoListIterator it(*list);
00266 QFileInfo *fi;
00267 int index = 0;
00268 while ( (fi = it.current()) != 0 )
00269 {
00270 if (fi->fileName() != ".")
00271 {
00272 m_directoryList.append(fi->fileName());
00273
00274
00275 UIListBtnTypeItem* item = new UIListBtnTypeItem(
00276 m_fileList, fi->fileName());
00277 item->setCheckable(false);
00278 item->setPixmap(m_directoryPixmap);
00279 item->setData((void*) index++);
00280 }
00281 ++it;
00282 }
00283 }
00284
00285 m_locationEdit->setText(m_curDirectory);
00286
00287 m_fileList->refresh();
00288 }