1 #include <CtrlLib/CtrlLib.h>
2 #include <Core/SSH/SSH.h>
3 
4 using namespace Upp;
5 
6 // SFtpFileSel:
7 // This example demonstrates FileSel integration of SFtp class,
8 // using FileSystemInfo interface.
9 
10 GUI_APP_MAIN
11 {
12 	const char *url = "demo:password@test.rebex.net:22";
13 
14 	Progress pi;
15 	pi.Title("Connecting to ssh server");
16 	pi.SetTotal(5);
17 	pi.Create();
18 
19 	bool refresh_gui = true; // FileSel refreshes its file list on every single GUI event.
20 	                         // In most cases this is not possible for an sftp connection,
21 	                         // because of the network latency. This switch is a workaround
22 	                         // for this particular problem, and allows the calls to the sftp
23 	                         // subsystem to be in sync with FileSel requests at the cost of a
24 	                         // minimal GUI lag in FileSel.
25 
26 	SshSession session;
__anon705f00f10102() 27 	session.WhenPhase = [&refresh_gui, &pi] (int phase) {
28 		switch(phase) {
29 		case SshSession::PHASE_DNS:
30 			pi.SetText("Resolving name...");
31 			pi.SetPos(1);
32 			break;
33 		case SshSession::PHASE_CONNECTION:
34 			pi.SetText("Connecting...");
35 			pi.SetPos(2);
36 			break;
37 		case SshSession::PHASE_HANDSHAKE:
38 			pi.SetText("Starting protocol handshake...");
39 			pi.SetPos(3);
40 			break;
41 		case SshSession::PHASE_AUTHORIZATION:
42 			pi.SetText("Authenticating...");
43 			pi.SetPos(4);
44 			break;
45 		case SshSession::PHASE_SUCCESS:
46 			pi.SetText("Client successfully connected to server.");
47 			pi.SetPos(5);
48 			pi.Close();
49 			refresh_gui = false;
50 			break;
51 		}
52 	};
__anon705f00f10202null53 	session.WhenWait = [&refresh_gui, &pi] { if(refresh_gui) pi.ProcessEvents(); };
54 	if(session.Timeout(30000).Connect(url)) {
55 		SFtp sftp(session);
56 		SFtpFileSystemInfo sfsi(sftp);
57 		FileSel fsel;
58 		fsel.Filesystem((FileSystemInfo&) sfsi);
59 		fsel.BaseDir(sftp.GetDefaultDir());
60 		while(fsel.Asking(false).ExecuteOpen("Select a file to download (Select cancel to quit)")) {
61 			pi.Reset();
62 			refresh_gui = true;
63 			String path = fsel.Get();
64 			sftp.WhenProgress = [&pi] (int64 done, int64 total)
__anon705f00f10302() 65 			{
66 				pi.SetText(
67 					Format("%s of %s is transferred",
68 						FormatFileSize(done),
69 						FormatFileSize(total)
70 					)
71 				);
72 				return pi.SetCanceled(int(done), int(total));
73 			};
74 			pi.Title("Downloading " << GetFileName(path));
75 			pi.Create();
76 			String f = sftp.LoadFile(path);
77 			if(sftp.IsError())
78 				ErrorOK(DeQtf(sftp.GetErrorDesc()));
79 			else
80 				SaveFile(NativePath(AppendFileName(GetCurrentDirectory(), GetFileName(path))), f);
81 			pi.Close();
82 			refresh_gui = false;
83 		}
84 	}
85 	else
86 		ErrorOK(DeQtf(session.GetErrorDesc()));
87 }
88