]> Creatis software - creaWT.git/blob - wt/bbtk_wt_PKG/src/bbwtFileUploader.cxx
2498 BBTK FeatureNewNormal wt-version PackageWt
[creaWT.git] / wt / bbtk_wt_PKG / src / bbwtFileUploader.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbwtFileUploader.h"
5 #include "bbwtPackage.h"
6 //#include <Wt/WFileUpload>
7 namespace bbwt
8 {
9
10         class FileUploaderWidget : public Wt::WContainerWidget
11         {
12                 public:
13               FileUploaderWidget(FileUploader* box,
14                                   Wt::WContainerWidget *parent,
15                                   Wt::WString label,
16                                   std::string pPath,
17                                   std::string fileName,
18                                   bool pNameIn);
19               ~FileUploaderWidget();
20               void fileUploaded();
21               void fileTooLarge();
22               void mUpload();
23             
24
25             private:
26               FileUploader* mBox;
27               std::string mPath;
28               std::string mName;
29               Wt::WPushButton *mwtPushButton;
30         Wt::WFileUpload *upload;
31         bool nameIn;
32         Wt::WLineEdit *name;
33
34         };
35
36   FileUploaderWidget::FileUploaderWidget(FileUploader* box,
37                                   Wt::WContainerWidget *parent,
38                                   Wt::WString label,
39                                   std::string pPath,
40                                   std::string fileName,
41                                   bool pNameIn)
42                       :
43                       Wt::WContainerWidget(parent),
44                       mBox(box),
45                       mPath(pPath),
46                       mName(fileName),
47                       nameIn(pNameIn)
48   {
49
50
51           Wt::WContainerWidget* w = this;
52           Wt::WVBoxLayout *vLay = new Wt::WVBoxLayout(); 
53           Wt::WVBoxLayout *vLay1 = new Wt::WVBoxLayout(); 
54           Wt::WHBoxLayout *hLay1 = new Wt::WHBoxLayout(); 
55           Wt::WContainerWidget* aux1 = new Wt::WContainerWidget();
56           Wt::WHBoxLayout *hLay2 = new Wt::WHBoxLayout(); 
57           Wt::WContainerWidget* aux2 = new Wt::WContainerWidget();
58           Wt::WContainerWidget* aux3 = new Wt::WContainerWidget();
59           upload = new Wt::WFileUpload(0);
60           upload->setFileTextSize(40);
61           // Provide a button
62           Wt::WPushButton *uploadButton = new Wt::WPushButton("Upload", 0);
63           // Upload when the button is clicked.
64           uploadButton->clicked().connect(upload, &Wt::WFileUpload::upload);
65           //uploadButton->clicked().connect(uploadButton, &Wt::WPushButton::disable);
66           // Upload automatically when the user entered a file.
67           //upload->changed().connect(upload, &Wt::WFileUpload::upload);
68           //upload->changed().connect(uploadButton, &Wt::WPushButton::disable);
69           // React to a succesfull upload.
70           upload->uploaded().connect(this, &FileUploaderWidget::fileUploaded);
71           // React to a fileupload problem.
72           //upload->fileTooLarge().connect(this, &FileUploaderWidget::fileTooLarge);
73     
74     vLay->addWidget(new Wt::WText("Upload your file"));
75     hLay1->addWidget(upload,1);
76     hLay1->addWidget(uploadButton,1);
77     aux1->setLayout(hLay1);
78     
79     hLay2->addWidget(new Wt::WText("File Name"),1);
80     
81     
82     
83     name = new Wt::WLineEdit();
84     name->setText(bbtk::std2wt(mName));
85     name->setReadOnly(!nameIn);
86     
87     hLay2->addWidget(name,1);
88     
89     aux2->setLayout(hLay2);
90     
91     
92     vLay1->addWidget(aux1,1);
93     vLay1->addWidget(aux2,1);
94     
95     aux3->setLayout(vLay1);
96     vLay->addWidget(aux3);
97     
98     
99     w->setMaximumSize(350,150);
100     w->setLayout(vLay);
101     
102
103   }
104
105   FileUploaderWidget::~FileUploaderWidget()
106   {
107
108   }
109   
110   void   FileUploaderWidget::fileUploaded()
111   {
112     std::cout<<"FILEUPLOAD----UPLOADING-----"<<std::endl;
113     std::string mFilename = upload->spoolFileName(); 
114     std::vector<Wt::Http::UploadedFile> mFileContents = upload->uploadedFiles();
115     upload->stealSpooledFile();
116     std::string mContents;
117     mContents=mFileContents.data()->spoolFileName();
118     std::ifstream  src(mContents.c_str(), std::ios::binary);
119     std::string ppa;
120     if(bbtk::wt2std(name->text()) == "")
121     {
122       ppa=mPath + bbtk::wt2std(upload->clientFileName());
123     }
124     else
125     {
126       ppa = mPath + bbtk::wt2std(name->text());
127
128     }
129     std::ofstream  dst(ppa.c_str(),   std::ios::binary);
130     dst << src.rdbuf();    
131     std::cout<<"FILEUPLOAD---------"<<ppa<<std::endl;
132     mBox->updatePath(ppa);
133   }
134  /* void fileTooLarge()
135   {
136     std::cout<<"ERROR ---- bbwtFileUploader : File size too large"<<std::endl;
137   }
138   */
139 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wt,FileUploader)
140 BBTK_BLACK_BOX_IMPLEMENTATION(FileUploader,bbtk::WtBlackBox);
141 //===== 
142 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
143 //===== 
144 void FileUploader::Process()
145 {
146 /*
147    std::string msg;
148     if (bbGetInputTitle()!="")
149       {
150         msg = bbGetInputTitle()+": " + bbGetInputIn();
151       }
152     else
153       {
154         msg = bbGetInputIn();
155       }
156    ((wxStaticText*)bbGetOutputWidget())->SetLabel( bbtk::std2wx( msg ) ); 
157   */
158 }
159 //===== 
160
161 void FileUploader::updatePath(std::string sPath)
162 {
163   std::cout<<"DEBUG : ----- FileUpliader.cxx "<<std::endl<<"\t\t Nuevo Path : "<<sPath<<std::endl;
164   bbSetOutputOutPath(sPath);
165 }
166
167 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
168 //===== 
169 void FileUploader::CreateWidget(Wt::WContainerWidget* parent)
170 {
171
172   FileUploaderWidget *w = new FileUploaderWidget( this,
173                                  parent,
174                                  bbtk::std2wt(bbGetInputTitle()),
175                                  bbGetInputPath(),
176                                  bbGetInputNameFile(),
177                                  bbGetInputInName());
178
179
180    bbSetOutputWidget(w);
181   
182 }
183 //===== 
184 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
185 //===== 
186 void FileUploader::bbUserSetDefaultValues()
187 {
188   bbSetInputTitle("");
189   //bbSetInputPath("");
190     //Testing by Johann Felipe Gonzalez Avila, Delete this line and replace it with the path of resources given in the bbtkWtBlackBox.cxx file in the inicialization of the app CreateWindow.
191   bbSetInputPath("/home/gonzalez/Documents/pruebaResources/");
192   bbSetInputInName(true);
193   bbSetInputNameFile("");
194   bbSetOutputOutPath("");
195
196 }
197 //===== 
198 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
199 //===== 
200 void FileUploader::bbUserInitializeProcessing()
201 {
202
203 }
204 //===== 
205 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
206 //===== 
207 void FileUploader::bbUserFinalizeProcessing()
208 {
209
210 }
211 }
212 // EO namespace bbwt
213
214