-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui_basic.erl
464 lines (390 loc) · 17.4 KB
/
gui_basic.erl
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
%%%---------------------------------------------------------------------
%%% Created by: Jody Swartz
%%% Creation date: 2011-10-19
%%% Refactored date: 2011-11-03
%%%---------------------------------------------------------------------
%%% Description module downloading_manager
%%%---------------------------------------------------------------------
%%% This Module is the main controller module for the GUI.
%%%---------------------------------------------------------------------
%%% Exports
%%%---------------------------------------------------------------------
%%% start()
%%% Produces GUI application and the loop for handling all events
%%% in the application.
%%
%%%---------------------------------------------------------------------
-module(gui_basic).
-include_lib("wx/include/wx.hrl").
-export([start/0]).
-compile(export_all).
-define(ADVANCE_FUNCTIONS_START, 1001).
-define(ADVANCE_FUNCTIONS_END, 1002).
start() ->
State = create_window(),
process_flag(trap_exit, true),
loop(State, createUniqueId()).
%%--------------------------------------------------------------------
%% Function: createUniqueId/0
%% Purpose: Checks and Creates a unique id
%% Args: no args
%% Returns: Id
%%--------------------------------------------------------------------
createUniqueId() ->
check_length("-JB1010-").
%%--------------------------------------------------------------------
%% Function: check_length/1
%% Purpose: Checks the length of the until it is 20bits, used throught the
%% duration of the application. Adds a random number at the end of the unique id.
%% Args: Id
%% Returns: Id
%%--------------------------------------------------------------------
check_length(Id) when length(Id) =:= 20 ->
Id;
check_length(Id) ->
check_length(Id ++ integer_to_list(random:uniform(9))).
%%--------------------------------------------------------------------
%% Function: create_window/0
%% Purpose: This creates the window frame
%% Args: no args
%% Returns: Frame, StatusBar, ToolBar, Text, NormalGauge, CalcText, TimeText,
%% TrackText, Advance_Start, Advance_End, TorNameText1201, CreateText1104,
%% CompleteText1105, But101, StaticBitmap_Dl, HashText1106
%%--------------------------------------------------------------------
create_window() ->
Wx = wx:new(),
Frame = wxFrame:new(Wx,
-1, % window id
"Glögg - BitTorrent Downloader ", % window title
[{size, {600, 180}}, {style,
?wxDEFAULT_FRAME_STYLE band bnot ?wxRESIZE_BORDER band bnot ?wxMAXIMIZE_BOX}]),
%% ICON for Frame
Icon = wxIcon:new("glogg.ico", [{type, ?wxBITMAP_TYPE_ICO}]),
wxFrame:setIcon(Frame, Icon),
%%Create StatusBar
StatusBar = wxFrame:createStatusBar(Frame,[{style, ?wxSB_RAISED}]),
wxStatusBar:setFieldsCount(StatusBar, 4),
wxFrame:connect(Frame, close_window),
wxTopLevelWindow:setSizeHints(Frame, 600, 180, [{maxW, 600}, {maxH, 500}]),
%%ToolBar
ToolBar = wxFrame:createToolBar(Frame,[]),
Open = wxBitmap:new("open.xpm", [{type, ?wxBITMAP_TYPE_XPM}]),
Help = wxBitmap:new("help.xpm", [{type, ?wxBITMAP_TYPE_XPM}]),
Info = wxBitmap:new("info.xpm", [{type, ?wxBITMAP_TYPE_XPM}]),
Advance_Start = wxBitmap:new("config1.xpm", [{type, ?wxBITMAP_TYPE_XPM}]),
Advance_End = wxBitmap:new("config2.xpm", [{type, ?wxBITMAP_TYPE_XPM}]),
wxToolBar:insertSeparator(ToolBar, 0),
wxToolBar:addTool(ToolBar, ?wxID_OPEN, "Open", Open,
[{shortHelp, "Open"}]),
wxToolBar:setToolLongHelp(ToolBar, ?wxID_OPEN, "Open's a torrent"),
wxToolBar:insertSeparator(ToolBar, 2),
wxToolBar:addCheckTool(ToolBar, ?ADVANCE_FUNCTIONS_END, "Advance",
Advance_Start, [{shortHelp, "Advance"},
{longHelp, "Activate Advance Function"}]),
wxToolBar:insertSeparator(ToolBar, 4),
wxToolBar:addTool(ToolBar, ?wxID_HELP, "Help", Help ,
[{shortHelp, "Help"}]),
wxToolBar:setToolLongHelp(ToolBar, ?wxID_HELP, "Help function"),
wxToolBar:insertSeparator(ToolBar, 6),
wxToolBar:addTool(ToolBar, ?wxID_ABOUT, "About", Info,
[{shortHelp, "Info"}]),
wxToolBar:setToolLongHelp(ToolBar, ?wxID_ABOUT, "Application Information"),
wxToolBar:insertSeparator(ToolBar, 8),
wxToolBar:realize(ToolBar),
wxFrame:setToolBar(Frame, ToolBar),
MainPanel = wxPanel:new(Frame, [{style, ?wxFULL_REPAINT_ON_RESIZE}]),
wxWindow:setBackgroundColour(MainPanel, ?wxWHITE),
OuterSizer = wxGridSizer:new(2, 1, 2, 2),
BasicSizer = wxBoxSizer:new(?wxVERTICAL),
wxSizer:add(OuterSizer, BasicSizer, [{proportion, 0}, {flag, ?wxEXPAND}]),
DL_Panel = wxPanel:new(MainPanel, [{winid, ?wxID_ANY},
{size, {100, 175}},
{style, ?wxFULL_REPAINT_ON_RESIZE}]),
%Download Icon
Image_Dl = wxImage:new("download.png", [{type, ?wxBITMAP_TYPE_PNG}]),
Bitmap_Dl = wxBitmap:new(Image_Dl, []),
StaticBitmap_Dl = wxStaticBitmap:new(DL_Panel, 2, Bitmap_Dl),
wxStaticBitmap:setToolTip(StaticBitmap_Dl, "Downloading Torrent"),
wxWindow:show(StaticBitmap_Dl, [{show, false}]),
%%Filename Text
Text = wxStaticText:new(DL_Panel, 1, "Filename",
[{style, ?wxALIGN_LEFT bor ?wxST_NO_AUTORESIZE}]),
%%Time Text
TimeText = wxStaticText:new(DL_Panel, 3, "Time ",
[{style, ?wxALIGN_LEFT bor ?wxST_NO_AUTORESIZE}]),
%Folder ICON
Image_Folder = wxImage:new("folder.png", [{type, ?wxBITMAP_TYPE_PNG}]),
Bitmap_Folder = wxBitmap:new(Image_Folder, []),
StaticBitmap_Folder = wxStaticBitmap:new(DL_Panel, 1, Bitmap_Folder),
%%Gauge
Range=100,
NormalGauge = wxGauge:new(DL_Panel, 1, Range, [{pos, {167, 45}},
{size, {200, -1}},
{style, ?wxGA_HORIZONTAL}]),
%Cancel Icon
Image_Cancel = wxImage:new("delete.png", [{type, ?wxBITMAP_TYPE_PNG}]),
Bitmap_Cancel = wxBitmap:new(Image_Cancel, []),
%%StaticBitmap_Cancel = wxStaticBitmap:new(DL_Panel, 3, Bitmap_Cancel),
But101 = wxBitmapButton:new(DL_Panel, 101, Bitmap_Cancel),
wxButton:setToolTip(But101, "Cancel Download"),
wxWindow:disable(But101),
%%Calculation Text
CalcText = wxStaticText:new(DL_Panel, 2, "Complete ",
[{style, ?wxALIGN_LEFT bor ?wxST_NO_AUTORESIZE}]),
%%URL Text
TrackText = wxStaticText:new(DL_Panel, 3, " - URL ",
[{style, ?wxALIGN_LEFT bor ?wxST_NO_AUTORESIZE}]),
StatGridSizer = wxGridSizer:new(1, 2, 1, 1),
wxSizer:add(StatGridSizer, CalcText, [{proportion, 0}, {flag, ?wxEXPAND}]),
wxSizer:add(StatGridSizer, TrackText,[{proportion, 0}, {flag, ?wxEXPAND}]),
wxSizer:layout(StatGridSizer),
%%Sizers
FlexGridSizer = wxFlexGridSizer:new(4, 3, 2, 2),
wxSizer:add(FlexGridSizer, 25, 15, []),
wxSizer:add(FlexGridSizer, 25, 15, []),
wxSizer:add(FlexGridSizer, 25, 15, []),
wxSizer:add(FlexGridSizer, StaticBitmap_Dl, [{proportion, 0},
{flag, ?wxALIGN_CENTER}]),
wxSizer:add(FlexGridSizer, Text, [{proportion, 0}, {flag, ?wxEXPAND}]),
wxSizer:add(FlexGridSizer, TimeText, [{proportion, 0},
{flag, ?wxALIGN_CENTER}]),
wxSizer:add(FlexGridSizer, StaticBitmap_Folder , [{proportion, 0},
{flag, ?wxALIGN_CENTER}]),
wxSizer:add(FlexGridSizer, NormalGauge, [{proportion, 0},
{flag, ?wxEXPAND}]),
wxSizer:add(FlexGridSizer, But101, [{proportion, 0},
{flag, ?wxALIGN_CENTER}]),
wxSizer:add(FlexGridSizer, 50, 30, []),
wxSizer:add(FlexGridSizer, StatGridSizer, [{proportion, 0},
{flag, ?wxEXPAND}]),
wxSizer:add(FlexGridSizer, 50, 30, []),
wxFlexGridSizer:addGrowableCol(FlexGridSizer, 1),
wxSizer:add(BasicSizer, DL_Panel, [{proportion, 0}, {flag, ?wxEXPAND}]),
Advance_sizer= wxBoxSizer:new(?wxVERTICAL),
wxSizer:add(OuterSizer, Advance_sizer,[{proportion, 0},
{flag, ?wxEXPAND}]),
wxSizer:layout(FlexGridSizer),
wxSizer:layout(BasicSizer),
wxSizer:layout(OuterSizer),
wxPanel:setSizer(DL_Panel, FlexGridSizer),
{TorNameText1201, CreateText1104, CompleteText1105, HashText1106, TpText1204, CpText1205} = gui_advance:start(MainPanel, OuterSizer),
wxPanel:setSizer(MainPanel, OuterSizer),
%%Connects widgets to events.
wxToolBar:connect(ToolBar, command_menu_selected, []),
wxToolBar:connect(ToolBar, command_left_click),
wxPanel:connect(MainPanel, command_button_clicked),
ok = wxFrame:setStatusText(Frame, "Welcome to Glögg!", [{number, 0}]),
wxWindow:show(Frame),
{Frame, StatusBar, ToolBar, Text, NormalGauge, CalcText, TimeText,
TrackText, Advance_Start, Advance_End, TorNameText1201, CreateText1104,
CompleteText1105, But101, StaticBitmap_Dl, HashText1106, TpText1204, CpText1205}.
%%--------------------------------------------------------------------
%% Function: loop/2
%% Purpose: This is the main loop for th GUI, which handles all events within
%% the GUI and recieved from the backend processes of the application.
%% Args: {Frame, StatusBar, ToolBar, Text, NormalGauge, CalcText, TimeText,
%% TrackText, Advance_Start, Advance_End, TorNameText1201, CreateText1104,
%%CompleteText1105, But101, StaticBitmap_Dl, HashText110}, Download_pid, States
%% Returns:loop(State, Download_Pid)
%%--------------------------------------------------------------------
loop(State, Download_pid) ->
%%Divides up the State, into variables to be used.
{Frame, StatusBar, ToolBar, Text, NormalGauge, CalcText, TimeText,
TrackText, Advance_Start, Advance_End, TorNameText1201, CreateText1104,
CompleteText1105, But101, StaticBitmap_Dl, HashText1106, TpText1204, CpText1205} = State,
receive
#wx{event=#wxClose{}} ->
io:format("~p Closing window ~n",[self()]),
ok = wxFrame:setStatusText(Frame, "Closing...",[]),
Download_pid ! stop,
wxWindow:destroy(State),
ok;
%%Handles the opening of Torrent File.
#wx{id= ?wxID_OPEN, event=#wxCommand{type=command_menu_selected}} ->
FC = wxFileDialog:new(Frame, [{style, ?wxFD_OPEN}]),
case wxDialog:showModal(FC) of
?wxID_OK ->
Path = wxFileDialog:getPath(FC),
Fname2 = filename:basename(Path),
case string:str(Fname2, ".torrent") of
0 ->
Error1 = wxMessageDialog:new(Frame,
"Invalid File Selection",
[{caption,
"Invalid File"},
{style, ?wxICON_ERROR}]),
wxMessageDialog:showModal(Error1),
loop(State, Download_pid);
_ ->
DownloadPid_new = download_manager:start(Path, %%Initializes the download manager
self()),
link(DownloadPid_new), %%Links the Downlaod Manager
wxWindow:show(StaticBitmap_Dl, [{show, true}]),
wxWindow:refresh(Frame),
wxWindow:enable(But101),
wxStaticText:setLabel(Text, Fname2),
wxToolBar:enableTool(ToolBar, ?wxID_OPEN, false),
Fname1 = "Filename: ",
TorrName = Fname1 ++ Fname2,
wxStaticText:setLabel(TorNameText1201, TorrName),
wxStaticText:setLabel(TimeText, create_time()),
Create_string = "Created on: " ++
create_date_and_time(),
wxWindow:refresh(Frame),
wxStaticText:setLabel(CreateText1104,
Create_string),
wxWindow:refresh(Frame),
wxStatusBar:setStatusText(StatusBar,
"Download Started...",
[{number, 0}]),
wxStatusBar:setStatusText(StatusBar, TorrName,
[{number, 1}]),
loop(State, DownloadPid_new)
end;
_ ->
ignore
end,
wxFileDialog:destroy(FC),
loop(State, Download_pid);
#wx{id= ?ADVANCE_FUNCTIONS_START,
event=#wxCommand{type=command_menu_selected}} ->
wxToolBar:removeTool(ToolBar, ?ADVANCE_FUNCTIONS_START),
wxToolBar:insertTool(ToolBar, 3, ?ADVANCE_FUNCTIONS_END,
"Advance", Advance_End, [{shortHelp,
"Advance"},
{longHelp,
"Activate Advance Function"}]),
wxToolBar:realize(ToolBar),
wxTopLevelWindow:setSize(Frame, 600, 190),
loop(State, Download_pid);
#wx{id= ?ADVANCE_FUNCTIONS_END,
event=#wxCommand{type=command_menu_selected}} ->
wxToolBar:removeTool(ToolBar, ?ADVANCE_FUNCTIONS_END),
wxToolBar:insertTool(ToolBar, 3, ?ADVANCE_FUNCTIONS_START,
"Advance", Advance_Start, [{shortHelp,
"Advance"},
{longHelp,
"Activate Advance Function"}]),
wxToolBar:realize(ToolBar),
wxTopLevelWindow:setSize(Frame, 600, 500),
loop(State, Download_pid);
{hash, {Hash, Total_pieces}} ->
Hash_string = "Hash: " ++ Hash,
wxStaticText:setLabel(HashText1106, Hash_string),
Total_pieces_string = "Total Pieces: " ++ integer_to_list(Total_pieces),
wxStaticText:setLabel(TpText1204, Total_pieces_string),
wxWindow:refresh(Frame),
loop(State, Download_pid);
{tracker, Track} ->
wxStaticText:setLabel(TrackText, Track),
wxWindow:refresh(Frame),
loop(State, Download_pid);
{percentage, {Percent, Dl_pieces}} ->
wxGauge:setValue(NormalGauge, Percent),
PText1 = integer_to_list(Percent),
PText2 = "% Complete",
StText = lists:concat([PText1, PText2]),
wxStaticText:setLabel(CalcText, StText),
wxStatusBar:setStatusText(StatusBar, StText, [{number, 3}]),
Pieces_left_string = "Pieces Left: " ++ integer_to_list(Dl_pieces),
wxStaticText:setLabel(CpText1205, Pieces_left_string),
case wxGauge:getValue(NormalGauge) of
100 ->
Date_time_string = "Completed on: " ++
create_date_and_time(),
wxStaticText:setLabel(CompleteText1105, Date_time_string),
Complete_dialog = wxMessageDialog:new(Frame,
"Succesfull Download!",
[{caption,
"Congratulations"},
{style, ?wxICON_EXCLAMATION}]),
wxMessageDialog:showModal(Complete_dialog);
_ ->
ignore
end,
wxWindow:refresh(Frame),
loop(State, Download_pid);
{'EXIT', _Pid, _Reason} ->
wxStatusBar:setStatusText(StatusBar,
"Cannot connect to Torrent Tracker",
[{number, 2}]),
Error2 = wxMessageDialog:new(Frame,
"Cannot connect to Torrent Tracker",
[{caption, "Connection timed out"},
{style, ?wxYES_NO}]),
case wxMessageDialog:showModal(Error2) of
?wxID_YES ->
loop(State, Download_pid);
?wxID_NO ->
wxToolBar:enableTool(ToolBar, ?wxID_OPEN, true),
wxStatusBar:setStatusText(StatusBar, " ", [{number, 1}]),
wxStatusBar:setStatusText(StatusBar, " ", [{number, 2}]),
wxStatusBar:setStatusText(StatusBar, "Welcome to Glögg!",
[{number, 0}])
end;
%%Handles the x button click.
#wx{id = 101, event=#wxCommand{type = command_button_clicked}} ->
wxGauge:setValue(NormalGauge, 0),
wxToolBar:enableTool(ToolBar, ?wxID_OPEN, true),
wxStaticText:setLabel(CalcText, " "),
wxStaticText:setLabel(Text, " "),
wxStaticText:setLabel(TimeText, " "),
wxStatusBar:setStatusText(StatusBar, " ", [{number, 1}]),
wxStatusBar:setStatusText(StatusBar, " ", [{number, 2}]),
wxStatusBar:setStatusText(StatusBar, "Welcome to Glögg!",
[{number, 0}]),
case Download_pid of
not_started ->
loop(State, not_started);
_ ->
spawn(fun() -> close_for_now(Download_pid) end),
loop(State, not_started)
end;
%%Handles the Help Event.
#wx{id= ?wxID_HELP, event=#wxCommand{type=command_menu_selected}} ->
wx_misc:launchDefaultBrowser("http://jjbeef.yolasite.com/"),
loop(State, Download_pid);
%%Handles About info
#wx{id= ?wxID_ABOUT, event=#wxCommand{type=command_menu_selected}} ->
gui_info:start(Frame),
loop(State, Download_pid);
_Msg ->
State,
loop(State, Download_pid)
end.
%%--------------------------------------------------------------------
%% Function: close_for_now/1
%% Purpose: Delays the stop for the Download_Pid
%% Args:
%% Returns:returns a stop for the Download_pid process ID.
%%--------------------------------------------------------------------
close_for_now(Download_pid) ->
receive
after 1000 ->
Download_pid ! stop
end.
%%--------------------------------------------------------------------
%% Function: create_time/1
%% Purpose: Creates a string of the current hours and minutes.
%% Args: no args
%% Returns:returns current time in in string format.
%%--------------------------------------------------------------------
create_time() ->
{H,M,_} = erlang:time(),
TText = integer_to_list(H) ++ ":" ++ integer_to_list(M),
TText.
%%--------------------------------------------------------------------
%% Function: create_date_and_time/1
%% Purpose: Creates a string representation of the current date and time.
%% Args: no args
%% Returns: returns the current date and time in string format.
%%--------------------------------------------------------------------
create_date_and_time() ->
{H,M,S} = erlang:time(),
{Y,Mo,D} = erlang:date(),
DText = integer_to_list(Y) ++ "-" ++ integer_to_list(Mo) ++ "-"
++ integer_to_list(D),
FText = integer_to_list(H) ++ ":" ++ integer_to_list(M) ++ ":"
++ integer_to_list(S),
Date_and_time = "Date: " ++ DText ++ " " ++ "Time: " ++ FText ++ " ",
Date_and_time.