00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00050 this->path = "/";
00051 this->filename = "";
00052
00053
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
00060 this->path = path;
00061 this->filename = filename;
00062
00063
00064 this->onOK = new sigc::signal<void, MMSFileDialog*>;
00065 this->onCancel = new sigc::signal<void>;
00066 }
00067
00068 MMSFileDialog::~MMSFileDialog() {
00069
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
00080 if (parent) {
00081
00082
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
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
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
00150 if (!isInitialized()) return false;
00151
00152
00153 if (this->filedialog_name)
00154 this->filedialog_name->setText(this->filename);
00155 fillMenu();
00156
00157
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
00167 window->hide();
00168
00169
00170 if (this->onOK)
00171 this->onOK->emit(this);
00172 }
00173 }
00174 else
00175 if (widget == this->filedialog_cancel) {
00176
00177 window->hide();
00178
00179
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
00188 int pos = (int)path.rfind("/");
00189 if (pos >= 0) {
00190
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
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
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
00244 if (!this->filedialog_filelist) return false;
00245 this->filedialog_filelist->clear();
00246
00247
00248 if (this->filedialog_path)
00249 this->filedialog_path->setText(path);
00250
00251
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
00263 MMSFileSearch *fs = new MMSFileSearch(this->path, "*", false, false, true);
00264 if (!fs) return false;
00265 MMSFILEENTRY_LIST list = fs->execute();
00266
00267
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
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
00296 this->filedialog_filelist->setSelected(0);
00297
00298 return true;
00299 }
00300
00301