source: PSPA/Interface_Web/trunk/pspaWT/sources/userInterface/src/GWt_serverFileSelector.cc @ 496

Last change on this file since 496 was 488, checked in by lemeur, 10 years ago

lecture fichier sauvegarde AML

File size: 5.8 KB
Line 
1#include <Wt/WVBoxLayout>
2#include <Wt/WHBoxLayout>
3#include <Wt/WImage>
4#include <Wt/WText>
5#include <Wt/WBreak>
6#include <Wt/WHBoxLayout>
7#include <Wt/WPushButton>
8#include <Wt/WCssDecorationStyle>
9#include <Wt/WTree>
10#include <Wt/WTreeNode>
11#include <Wt/WIconPair>
12#include <Wt/WApplication>
13
14
15#include <boost/algorithm/string.hpp>
16#include <dirent.h>             /* readdir(), etc.                    */
17#include <sys/stat.h>           /* stat(), etc.                       */
18
19#include "GWt_serverFileSelector.h"
20
21
22GWt_serverFileSelector::GWt_serverFileSelector(
23 WString titre,
24 std::string wd):
25GWt_dialog(titre, NULL, true)
26{
27   
28    workingDir_ = wd;
29       
30    Wt::WIconPair *folderIcon = new Wt::WIconPair("htdocs/yellow-folder-closed.png",
31                                                  "htdocs/yellow-folder-open.png", false);
32   
33    treeRoot_ = new Wt::WTree();
34    treeRoot_->setSelectionMode(Wt::SingleSelection);
35   
36    /* this structure is used for storing the name of each entry in turn. */
37    struct dirent* entry;
38   
39    /* read the directory's contents, print out the name of each entry.   */
40    DIR* dir = opendir(workingDir_.c_str());
41    if (!dir) {
42        exit(1);
43    }
44    struct stat dir_stat;       /* used by stat().                   */
45    while ( (entry = readdir(dir)) != NULL) {
46        /* check if the given entry is a directory. */
47        if (stat(entry->d_name, &dir_stat) == -1) {
48            continue;
49        }
50    }
51   
52   
53    Wt::WTreeNode* treeFileRoot = new Wt::WTreeNode("Workspace", folderIcon);
54
55    //    treeFileRoot->setStyleClass("example-tree");
56    treeRoot_->setTreeRoot(treeFileRoot);
57    treeFileRoot->label()->setTextFormat(Wt::PlainText);
58    treeFileRoot->setImagePack("resources/");
59    treeFileRoot->setLoadPolicy(Wt::WTreeNode::NextLevelLoading);
60   
61    //    buildConfigurationFileTree(treeFileRoot, workingDir_.c_str(), ".save");
62        buildConfigurationFileTree(treeFileRoot, workingDir_.c_str(), ".aml");
63
64    // Add user files
65    Wt::WTreeNode* userTreeFileRoot = new Wt::WTreeNode("MySession", folderIcon);
66    // buildConfigurationFileTree(treeFileRoot, (workingDir_+WApplication::instance()->sessionId()).c_str(), ".save");
67    buildConfigurationFileTree(treeFileRoot, (workingDir_+WApplication::instance()->sessionId()).c_str(), ".aml");
68
69    treeFileRoot->expand();
70    WContainerWidget *treeContainer = new WContainerWidget();
71    treeContainer->addWidget(treeRoot_);
72
73    WContainerWidget *buttonContainer = new WContainerWidget();
74    WHBoxLayout* hButtonLayout = new WHBoxLayout();
75   
76    WPushButton *annule = new WPushButton("cancel");
77    WPushButton *submit = new WPushButton("OK");
78    hButtonLayout->addWidget(annule);
79    hButtonLayout->addWidget(submit);
80   
81    annule->clicked().connect(this, &Wt::WDialog::reject);
82    submit->clicked().connect(this, &Wt::WDialog::accept);
83   
84    buttonContainer->setLayout(hButtonLayout);
85
86    // add the container already present
87   
88    addVContent(treeContainer);
89    addVContent(buttonContainer);
90   
91    treeContainer->resize(400,300);
92    treeContainer->setOverflow(WContainerWidget::OverflowAuto);
93
94}
95
96
97
98/*
99 * function: findfile. recusively traverse the current directory, searching
100 *                     for files with a given string in their name.
101 * input:    string to match.
102 * output:   any file found, printed to the screen with a full path.
103 */
104void GWt_serverFileSelector::buildConfigurationFileTree(Wt::WTreeNode *treeFileRoot, const char* folder, const char* pattern)
105{
106#define MAX_DIR_PATH 2048 /* maximal full path we support.      */
107   
108    DIR* dir;   /* pointer to the scanned directory. */
109    struct dirent* entry; /* pointer to one directory entry.   */
110    char cwd[MAX_DIR_PATH+1]; /* current working directory.        */
111    struct stat dir_stat;       /* used by stat().                   */
112   
113    /* first, save path of current working directory */
114    if (!getcwd(cwd, MAX_DIR_PATH+1)) {
115        return;
116    }
117   
118   
119    /* open the directory for reading */
120    dir = opendir(folder);
121    if (!dir) {
122        return;
123    }
124   
125    /* scan the directory, traversing each sub-directory, and */
126    /* matching the pattern for each file name.               */
127    while ((entry = readdir(dir))) {
128       
129        /* skip the "." and ".." entries, to avoid loops. */
130        if (strcmp(entry->d_name, ".") == 0)
131            continue;
132        if (strcmp(entry->d_name, "..") == 0)
133            continue;
134        if (strcmp(entry->d_name, ".svn") == 0)
135            continue;
136        /* check if the given entry is a directory. */
137        if (entry->d_type == DT_DIR) {
138            Wt::WIconPair *folderIcon = new Wt::WIconPair("htdocs/yellow-folder-closed.png",
139                                                          "htdocs/yellow-folder-open.png", false);
140            Wt::WTreeNode *child = new Wt::WTreeNode(entry->d_name, folderIcon);
141            treeFileRoot->addChildNode(child);
142            buildConfigurationFileTree(child,(std::string(folder)+entry->d_name).c_str(), pattern);
143           
144        } else {
145           
146            /* check if the pattern matchs. */
147            if (boost::algorithm::ends_with(entry->d_name, pattern)) {
148                treeFileRoot->addChildNode(new Wt::WTreeNode(entry->d_name));
149            }
150        }
151    }
152}
153
154std::string GWt_serverFileSelector::exec() {
155   
156    Wt::WDialog::DialogCode dc = WDialog::exec();
157    if (dc == Rejected) {
158        return "";
159    }
160    const Wt::WTree::WTreeNodeSet& tr = treeRoot_->selectedNodes();
161    if (tr.empty()) {
162        return "";
163    }
164    std::string path = (*tr.begin())->label()->text().toUTF8();
165
166    Wt::WTreeNode* parent = (*tr.begin())->parentNode();
167    while (parent) {
168        parent = parent->parentNode();
169        if( parent) {
170            path = parent->label()->text().toUTF8()+"/"+path;
171        }
172    }
173    return workingDir_+path;
174}
Note: See TracBrowser for help on using the repository browser.