#include #include #include #include #include #include #include #include #include #include #include #include #include #include /* readdir(), etc. */ #include /* stat(), etc. */ #include "GWt_serverFileSelector.h" GWt_serverFileSelector::GWt_serverFileSelector( WString titre, std::string wd): GWt_dialog(titre, NULL, true) { workingDir_ = wd; Wt::WIconPair *folderIcon = new Wt::WIconPair("htdocs/yellow-folder-closed.png", "htdocs/yellow-folder-open.png", false); treeRoot_ = new Wt::WTree(); treeRoot_->setSelectionMode(Wt::SingleSelection); /* this structure is used for storing the name of each entry in turn. */ struct dirent* entry; /* read the directory's contents, print out the name of each entry. */ DIR* dir = opendir(workingDir_.c_str()); if (!dir) { exit(1); } struct stat dir_stat; /* used by stat(). */ while ( (entry = readdir(dir)) != NULL) { /* check if the given entry is a directory. */ if (stat(entry->d_name, &dir_stat) == -1) { continue; } } Wt::WTreeNode* treeFileRoot = new Wt::WTreeNode("Workspace", folderIcon); treeFileRoot->setStyleClass("example-tree"); treeRoot_->setTreeRoot(treeFileRoot); treeFileRoot->label()->setTextFormat(Wt::PlainText); treeFileRoot->setImagePack("resources/"); treeFileRoot->setLoadPolicy(Wt::WTreeNode::NextLevelLoading); buildConfigurationFileTree(treeFileRoot, workingDir_.c_str(), ".save"); // Add user files Wt::WTreeNode* userTreeFileRoot = new Wt::WTreeNode("MySession", folderIcon); buildConfigurationFileTree(treeFileRoot, (workingDir_+WApplication::instance()->sessionId()).c_str(), ".save"); treeFileRoot->expand(); WContainerWidget *treeContainer = new WContainerWidget(); treeContainer->addWidget(treeRoot_); WContainerWidget *buttonContainer = new WContainerWidget(); WHBoxLayout* hButtonLayout = new WHBoxLayout(); WPushButton *annule = new WPushButton("cancel"); WPushButton *submit = new WPushButton("OK"); hButtonLayout->addWidget(annule); hButtonLayout->addWidget(submit); annule->clicked().connect(this, &Wt::WDialog::reject); submit->clicked().connect(this, &Wt::WDialog::accept); buttonContainer->setLayout(hButtonLayout); // add the container already present addVContent(treeContainer); addVContent(buttonContainer); treeContainer->resize(400,300); treeContainer->setOverflow(WContainerWidget::OverflowAuto); } /* * function: findfile. recusively traverse the current directory, searching * for files with a given string in their name. * input: string to match. * output: any file found, printed to the screen with a full path. */ void GWt_serverFileSelector::buildConfigurationFileTree(Wt::WTreeNode *treeFileRoot, const char* folder, const char* pattern) { #define MAX_DIR_PATH 2048 /* maximal full path we support. */ DIR* dir; /* pointer to the scanned directory. */ struct dirent* entry; /* pointer to one directory entry. */ char cwd[MAX_DIR_PATH+1]; /* current working directory. */ struct stat dir_stat; /* used by stat(). */ /* first, save path of current working directory */ if (!getcwd(cwd, MAX_DIR_PATH+1)) { return; } /* open the directory for reading */ dir = opendir(folder); if (!dir) { return; } /* scan the directory, traversing each sub-directory, and */ /* matching the pattern for each file name. */ while ((entry = readdir(dir))) { /* skip the "." and ".." entries, to avoid loops. */ if (strcmp(entry->d_name, ".") == 0) continue; if (strcmp(entry->d_name, "..") == 0) continue; if (strcmp(entry->d_name, ".svn") == 0) continue; /* check if the given entry is a directory. */ if (entry->d_type == DT_DIR) { Wt::WIconPair *folderIcon = new Wt::WIconPair("htdocs/yellow-folder-closed.png", "htdocs/yellow-folder-open.png", false); Wt::WTreeNode *child = new Wt::WTreeNode(entry->d_name, folderIcon); treeFileRoot->addChildNode(child); buildConfigurationFileTree(child,(std::string(folder)+entry->d_name).c_str(), pattern); } else { /* check if the pattern matchs. */ if (boost::algorithm::ends_with(entry->d_name, pattern)) { treeFileRoot->addChildNode(new Wt::WTreeNode(entry->d_name)); } } } } std::string GWt_serverFileSelector::exec() { Wt::WDialog::DialogCode dc = WDialog::exec(); if (dc == Rejected) { return ""; } const Wt::WTree::WTreeNodeSet& tr = treeRoot_->selectedNodes(); if (tr.empty()) { return ""; } std::string path = (*tr.begin())->label()->text().toUTF8(); Wt::WTreeNode* parent = (*tr.begin())->parentNode(); while (parent) { parent = parent->parentNode(); if( parent) { path = parent->label()->text().toUTF8()+"/"+path; } } return workingDir_+path; }