Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsfiledialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #include "mmsgui/additional/mmsfiledialog.h"
00034 #include "mmsinfo/mmsinfo.h"
00035 #include "mmstools/mmsfilesearch.h"
00036 
00037 #define FILEDIALOG_TITLE    "filedialog_title"
00038 #define FILEDIALOG_OK       "filedialog_ok"
00039 #define FILEDIALOG_CANCEL   "filedialog_cancel"
00040 #define FILEDIALOG_PATH     "filedialog_path"
00041 #define FILEDIALOG_NAME     "filedialog_name"
00042 #define FILEDIALOG_FILELIST "filedialog_filelist"
00043 #define PATH_OR_FILE        "pof"
00044 #define FILEDIALOG_UP       "filedialog_up"
00045 #define FILEDIALOG_DOWN     "filedialog_down"
00046 
00047 
00048 MMSFileDialog::MMSFileDialog(MMSWindow *window) {
00049     // init
00050     this->path = "/";
00051     this->filename = "";
00052 
00053     // initialize the callbacks
00054     this->onOK     = new sigc::signal<void, MMSFileDialog*>;
00055     this->onCancel = new sigc::signal<void>;
00056 }
00057 
00058 MMSFileDialog::MMSFileDialog(string path, string filename, MMSWindow *window) : MMSGUIControl(window) {
00059     // init
00060     this->path = path;
00061     this->filename = filename;
00062 
00063     // initialize the callbacks
00064     this->onOK     = new sigc::signal<void, MMSFileDialog*>;
00065     this->onCancel = new sigc::signal<void>;
00066 }
00067 
00068 MMSFileDialog::~MMSFileDialog() {
00069     // delete the callbacks
00070     if (this->onOK)
00071         delete this->onOK;
00072     if (this->onCancel)
00073         delete this->onCancel;
00074 }
00075 
00076 
00077 bool MMSFileDialog::load(MMSWindow *parent, string dialogfile, MMSTheme *theme) {
00078     if (!MMSGUIControl::load(parent, dialogfile, theme)) {
00079         // base class has failed to load...
00080         if (parent) {
00081             // load the default dialog file which includes a child window
00082             // do this only if a parent window is given!!!
00083             this->window = this->dm->loadChildDialog((string)getPrefix() + "/share/disko/mmsgui/mmsfiledialog.xml", theme);
00084         }
00085     }
00086 
00087     if (!this->window)
00088         return false;
00089 
00090     // get access to the widgets
00091     this->filedialog_title = (MMSLabelWidget*)this->window->findWidget(FILEDIALOG_TITLE);
00092     this->filedialog_ok = this->window->findWidget(FILEDIALOG_OK);
00093     this->filedialog_cancel = this->window->findWidget(FILEDIALOG_CANCEL);
00094     this->filedialog_path = (MMSLabelWidget*)this->window->findWidget(FILEDIALOG_PATH);
00095     this->filedialog_name = (MMSInputWidget*)this->window->findWidget(FILEDIALOG_NAME);
00096     this->filedialog_filelist = (MMSMenuWidget*)this->window->findWidget(FILEDIALOG_FILELIST);
00097     this->filedialog_up = (MMSButtonWidget*)this->window->findWidget(FILEDIALOG_UP);
00098     this->filedialog_down = (MMSButtonWidget*)this->window->findWidget(FILEDIALOG_DOWN);
00099 
00100     // check something and/or connect callbacks if widgets does exist
00101     if (this->filedialog_title)
00102         if (this->filedialog_title->getType() != MMSWIDGETTYPE_LABEL)
00103             this->filedialog_title = NULL;
00104     if (this->filedialog_ok)
00105         if (this->filedialog_ok->getType() == MMSWIDGETTYPE_BUTTON)
00106             this->filedialog_ok->onReturn->connect(sigc::mem_fun(this,&MMSFileDialog::onReturn));
00107     if (this->filedialog_cancel)
00108         if (this->filedialog_cancel->getType() == MMSWIDGETTYPE_BUTTON)
00109             this->filedialog_cancel->onReturn->connect(sigc::mem_fun(this,&MMSFileDialog::onReturn));
00110     if (this->filedialog_path)
00111         if (this->filedialog_path->getType() != MMSWIDGETTYPE_LABEL)
00112             this->filedialog_path = NULL;
00113     if (this->filedialog_name)
00114         if (this->filedialog_name->getType() != MMSWIDGETTYPE_INPUT)
00115             this->filedialog_name = NULL;
00116     if (this->filedialog_filelist) {
00117         if (this->filedialog_filelist->getType() == MMSWIDGETTYPE_MENU) {
00118             this->filedialog_filelist->onReturn->connect(sigc::mem_fun(this,&MMSFileDialog::onReturn));
00119             this->filedialog_filelist->onSelectItem->connect(sigc::mem_fun(this,&MMSFileDialog::onSelectItem));
00120         }
00121         else
00122             this->filedialog_filelist = NULL;
00123     }
00124     if (this->filedialog_up) {
00125         if (this->filedialog_up->getType() == MMSWIDGETTYPE_BUTTON)
00126             this->filedialog_up->onReturn->connect(sigc::mem_fun(this,&MMSFileDialog::onReturn));
00127         else
00128             this->filedialog_up = NULL;
00129     }
00130     if (this->filedialog_down) {
00131         if (this->filedialog_down->getType() == MMSWIDGETTYPE_BUTTON)
00132             this->filedialog_down->onReturn->connect(sigc::mem_fun(this,&MMSFileDialog::onReturn));
00133         else
00134             this->filedialog_down = NULL;
00135     }
00136 
00137     return true;
00138 }
00139 
00140 bool MMSFileDialog::setTitle(string title) {
00141     if (filedialog_title) {
00142         filedialog_title->setText(title);
00143         return true;
00144     }
00145     return false;
00146 }
00147 
00148 bool MMSFileDialog::show() {
00149     // initialized?
00150     if (!isInitialized()) return false;
00151 
00152     // re-init the dialog
00153     if (this->filedialog_name)
00154         this->filedialog_name->setText(this->filename);
00155     fillMenu();
00156 
00157     // show the dialog
00158     this->window->setFocus();
00159 
00160     return true;
00161 }
00162 
00163 void MMSFileDialog::onReturn(MMSWidget *widget) {
00164     if (widget == this->filedialog_ok) {
00165         if (this->filename!="") {
00166             // hide the window
00167             window->hide();
00168 
00169             // call callback
00170             if (this->onOK)
00171                 this->onOK->emit(this);
00172         }
00173     }
00174     else
00175     if (widget == this->filedialog_cancel) {
00176         // hide the window
00177         window->hide();
00178 
00179         // call callback
00180         if (this->onCancel)
00181             this->onCancel->emit();
00182     }
00183     else
00184     if (widget == this->filedialog_filelist) {
00185         unsigned int sel = this->filedialog_filelist->getSelected();
00186         if ((sel==0)&&(this->path != "/")) {
00187             // back item
00188             int pos = (int)path.rfind("/");
00189             if (pos >= 0) {
00190                 // go to the parent dir
00191                 this->path = this->path.substr(0, pos);
00192                 if (this->path=="") this->path="/";
00193                 fillMenu();
00194             }
00195         }
00196         else {
00197             MMSWidget *item = this->filedialog_filelist->getItem(sel);
00198             if (item) {
00199                 string data;
00200                 if (item->getData(data)) {
00201                     if ((int)data.find("D.") == 0) {
00202                         // go to the subdir
00203                         this->path = data.substr(2);
00204                         fillMenu();
00205                     }
00206                     else
00207                     if ((int)data.find("F.") == 0) {
00208                         DEBUGOUT("file = %s\n", data.c_str());
00209                     }
00210                 }
00211             }
00212         }
00213     }
00214     else
00215     if (widget == this->filedialog_up) {
00216         if (this->filedialog_filelist)
00217             this->filedialog_filelist->scrollUp(1,true,false,true);
00218     }
00219     else
00220     if (widget == this->filedialog_down) {
00221         if (this->filedialog_filelist)
00222             this->filedialog_filelist->scrollDown(1,true,false,true);
00223     }
00224 }
00225 
00226 void MMSFileDialog::onSelectItem(MMSWidget *widget) {
00227     if (!this->filedialog_name)
00228         return;
00229 
00230     // set the name of the file
00231     string data;
00232     if (widget->getData(data))
00233         if ((int)data.find("F.") == 0) {
00234             int pos = (int)data.rfind("/");
00235             if (pos > 0) {
00236                 this->filename = data.substr(pos+1);
00237                 this->filedialog_name->setText(this->filename);
00238             }
00239         }
00240 }
00241 
00242 bool MMSFileDialog::fillMenu() {
00243     // check and clear
00244     if (!this->filedialog_filelist) return false;
00245     this->filedialog_filelist->clear();
00246 
00247     // update path
00248     if (this->filedialog_path)
00249         this->filedialog_path->setText(path);
00250 
00251     // create back item
00252     if (this->path != "/") {
00253         MMSWidget *item = filedialog_filelist->newItem();
00254         if (item) {
00255             MMSLabelWidget *label = (MMSLabelWidget*)item->findWidget(PATH_OR_FILE);
00256             if ((label)&&(label->getType() == MMSWIDGETTYPE_LABEL)) {
00257                 label->setText("[..]");
00258             }
00259         }
00260     }
00261 
00262     // searching for files and directories
00263     MMSFileSearch *fs = new MMSFileSearch(this->path, "*", false, false, true);
00264     if (!fs) return false;
00265     MMSFILEENTRY_LIST list = fs->execute();
00266 
00267     // create menu items - directories
00268     for(MMSFILEENTRY_LIST::iterator it = list.begin();it!=list.end();it++) {
00269         if ((*it)->isdir) {
00270             MMSWidget *item = filedialog_filelist->newItem();
00271             if (item) {
00272                 MMSLabelWidget *label = (MMSLabelWidget*)item->findWidget(PATH_OR_FILE);
00273                 if ((label)&&(label->getType() == MMSWIDGETTYPE_LABEL)) {
00274                     label->setText("[" + (*it)->basename + "]");
00275                     item->setData("D." + (*it)->name);
00276                 }
00277             }
00278         }
00279     }
00280 
00281     // create menu items - files
00282     for(MMSFILEENTRY_LIST::iterator it = list.begin();it!=list.end();it++) {
00283         if (!(*it)->isdir) {
00284             MMSWidget *item = filedialog_filelist->newItem();
00285             if (item) {
00286                 MMSLabelWidget *label = (MMSLabelWidget*)item->findWidget(PATH_OR_FILE);
00287                 if ((label)&&(label->getType() == MMSWIDGETTYPE_LABEL)) {
00288                     label->setText((*it)->basename);
00289                     item->setData("F." + (*it)->name);
00290                 }
00291             }
00292         }
00293     }
00294 
00295     // selection to the first item
00296     this->filedialog_filelist->setSelected(0);
00297 
00298     return true;
00299 }
00300 
00301 

Generated by doxygen