-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScannerGUI.cpp
More file actions
348 lines (289 loc) · 8.78 KB
/
ScannerGUI.cpp
File metadata and controls
348 lines (289 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <gtkmm.h>
#include <iostream>
#include <deque>
#include <iostream>
#include <glibmm/threads.h>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>
#include "telnet.h"
#ifdef POSIX
#include <termios.h>
#endif
using boost::asio::ip::tcp;
using namespace std;
class ScannerGUI;//forward declaration
class Worker{
private:
ScannerGUI* mainThread;
Glib::Threads::Mutex mutex;
deque<string> tx;
deque<string> rx;
void _processFiles();
void addTextToTextArea(string text);
void showProgress(string text, double progress);
public:
Worker(ScannerGUI* mainThread);
void serverCommunicationThread();
Glib::Dispatcher updateTextArea;
Glib::Dispatcher updateProgress;
string textAreaMessage;
string progressMessage;
double progressFraction;
void processFiles();
string folderToProcess;
bool stop;
};
class ScannerGUI : public Gtk::Window
{
public:
ScannerGUI();
virtual ~ScannerGUI();
Glib::Threads::Mutex mainMutex;
void updateTextAreaMain();
void updateProgressMain();
Glib::Dispatcher doScan;
protected:
//Signal handlers:
void on_button_clicked();
Glib::ustring x;
Worker *worker;
//Member widgets:
Gtk::Box m_box1;
Gtk::Button m_button1;
Gtk::TextView m_TextView;
Gtk::ScrolledWindow m_ScrolledWindow;
Glib::RefPtr<Gtk::TextBuffer> m_refTextBuffer1;
Glib::Threads::Thread * m_thread;
Gtk::Button m_button2;
Gtk::ProgressBar m_progress;
void addTextToTextArea(string text);
void updateProgress(string text, double progress);
Glib::Dispatcher sig_message;
private:
string folderToProcess;
};
void ScannerGUI::updateTextAreaMain(){
Glib::Threads::Mutex::Lock lock (mainMutex);
m_refTextBuffer1->begin_user_action();
m_refTextBuffer1->insert_interactive_at_cursor(worker->textAreaMessage);
m_refTextBuffer1->end_user_action();
}
void ScannerGUI::updateProgressMain(){
Glib::Threads::Mutex::Lock lock (mainMutex);
m_progress.set_text(worker->progressMessage);
m_progress.set_fraction(worker->progressFraction);
}
void Worker::addTextToTextArea(string text){
Glib::Threads::Mutex::Lock lock (mutex);
this->textAreaMessage = text;
updateTextArea();
}
void Worker::showProgress(string text, double progress){
Glib::Threads::Mutex::Lock lock (mutex);
this->progressMessage = text;
this->progressFraction = progress;
updateProgress();
}
void Worker::processFiles(){
Glib::Threads::Thread::create( sigc::mem_fun(*this,&Worker::_processFiles));
}
void Worker::_processFiles(){
using namespace boost::filesystem;
rx.clear();
path current_dir(folderToProcess); //
vector<string> filesToScan;
for (recursive_directory_iterator iter(current_dir), end; iter != end; ++iter){
if( boost::filesystem::is_regular_file( iter->path() ))
filesToScan.push_back(iter->path().c_str());
}
for(size_t i = 0; i < filesToScan.size(); i++) {
string s = filesToScan[i];
double progress = i;
progress /= filesToScan.size();
showProgress(s, progress);
tx.push_back("scanfile \""+s+"\"\n");
while( rx.size() == 0 )
Glib::usleep(10000);
string str = rx.front();
rx.pop_front();
//std::cout << "RX: "<< str << endl;
}
showProgress("Done",1);
}
Worker::Worker(ScannerGUI* mainThread){
this->mainThread = mainThread;
this->stop = false;
}
void Worker::serverCommunicationThread()
{
// on Unix POXIS based systems, turn off line buffering of input, so cin.get() returns after every keypress
// On other systems, you'll need to look for an equivalent
#ifdef POSIX
termios stored_settings;
tcgetattr(0, &stored_settings);
termios new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_lflag &= (~ISIG); // don't automatically handle control-C
tcsetattr(0, TCSANOW, &new_settings);
#endif
try{
boost::asio::io_service io_service;
// resolve the host name and port number to an iterator that can be used to connect to the server
tcp::resolver resolver(io_service);
addTextToTextArea("Connecting to localhost port 65001...\n");
tcp::resolver::query query("localhost", "65001");
tcp::resolver::iterator iterator = resolver.resolve(query);
// define an instance of the main class of this program
telnet_client c(io_service, iterator);
// run the IO service as a separate thread, so the main thread can block on standard input
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
long counter = 0;
while (1)
{
string str;
counter++;
bool dataRx = false;
// Read from server
c.mtx_.lock();
while(c.readque.size() != 0){
str += c.readque.front();
c.readque.pop_front();
dataRx = true;
}
c.mtx_.unlock();
// Write responce to our waiting thread
if( dataRx ){
Glib::Threads::Mutex::Lock lock (mutex);
rx.push_back(str);
}
// Update text buffer
if( dataRx ){
addTextToTextArea(str);
}
//Write to server
if( tx.size() != 0 ){
{
Glib::Threads::Mutex::Lock lock (mutex);
str = tx.front();
tx.pop_front();
}
//cout << "tx: " << str << endl;
addTextToTextArea(str);
c.mtx_.lock();
for(size_t i = 0; i < str.length(); i++){
c.write(str[i]);
}
c.mtx_.unlock();
}
Glib::usleep(10000);
str = "";
if(stop){
Glib::Threads::Mutex::Lock lock (mutex);
c.close();
t.join();
return;
}
}
c.close(); // close the telnet client connection
t.join(); // wait for the IO service thread to close
}catch(exception& e){
cerr << "Exception: " << e.what() << "\n";
}
#ifdef POSIX // restore default buffering of standard input
tcsetattr(0, TCSANOW, &stored_settings);
#endif
}
int main (int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
if(!Glib::thread_supported()) Glib::thread_init();
ScannerGUI scannerGui;
//Shows the window and returns when it is closed.
return app->run(scannerGui);
}
ScannerGUI::ScannerGUI():
m_button1("Select folder to scan"), // creates a new button with label "Hello World".
m_button2("hello"),
m_box1(Gtk::ORIENTATION_VERTICAL)
{
//if(!Glib::thread_supported()) Glib::thread_init();
// Sets the border width of the window.
set_border_width(20);
set_title("Scanner (c) Nicolai Tufar, 2013");
set_position(Gtk::WindowPosition(Gtk::WIN_POS_CENTER));
set_default_size(600, 400);
// When the button receives the "clicked" signal, it will call the
// on_button_clicked() method defined below.
m_button1.signal_clicked().connect(sigc::mem_fun(*this,
&ScannerGUI::on_button_clicked));
m_box1.set_spacing(10);
add(m_box1);
m_box1.pack_start(m_button1, Gtk::PACK_SHRINK);
m_button1.show();
m_ScrolledWindow.add(m_TextView);
m_refTextBuffer1 = Gtk::TextBuffer::create();
//m_refTextBuffer1->set_text("This is the text from TextBuffer #1.");
m_TextView.set_buffer(m_refTextBuffer1);
m_TextView.show();
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
m_box1.pack_start(m_ScrolledWindow);
m_ScrolledWindow.show();
//m_box1.pack_start(m_button2);
//m_button2.show();
m_box1.pack_start(m_progress, Gtk::PACK_SHRINK);
//m_progress.set_default_size(570, 50);
m_progress.set_text("");
m_progress.set_show_text(true);
//m_progress.set_fraction(0.33);
m_progress.show();
m_box1.show();
worker = new Worker(this);
worker->updateTextArea.connect(sigc::mem_fun(*this, &ScannerGUI::updateTextAreaMain));
worker->updateProgress.connect(sigc::mem_fun(*this, &ScannerGUI::updateProgressMain));
this->doScan.connect(sigc::mem_fun(*worker, &Worker::processFiles));
m_thread = Glib::Threads::Thread::create( sigc::mem_fun(*worker,&Worker::serverCommunicationThread));
}
ScannerGUI::~ScannerGUI()
{
{
Glib::Threads::Mutex::Lock lock (mainMutex);
worker->stop = true;
}
if(m_thread)
m_thread->join();
}
void ScannerGUI::on_button_clicked()
{
Gtk::FileChooserDialog dialog("Please choose a folder",
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
dialog.set_transient_for(*this);
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button("Select", Gtk::RESPONSE_OK);
int result = dialog.run();
switch(result)
{
case(Gtk::RESPONSE_OK):
{
dialog.hide();
//std::cout << "Scanning: " << dialog.get_filename() << std::endl;
folderToProcess = dialog.get_filename();
worker->folderToProcess = folderToProcess;
this->doScan();
break;
}
case(Gtk::RESPONSE_CANCEL):
{
std::cout << "Cancel clicked." << std::endl;
break;
}
default:
{
// Dialog closed
break;
}
}
}