-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
186 lines (156 loc) · 6.2 KB
/
Main.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace telegram_exporter
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
// Re-populate adb text field if it was already set in the past.
string adbPathSetting = Properties.Settings.Default.AdbPath;
textBoxAdbPath.Text = adbPathSetting;
// Re-populates out directory where export will store files.
string outDirSetting = Properties.Settings.Default.OutDir;
textBoxOutDir.Text = outDirSetting;
// Shame: Resets empty labels.
labelDetectedDevices.Text = "";
labelTelegramDataCheck.Text = "";
labelCopyData.Text = "";
// Enable buttons if outdir or adb path are not selected.
if (adbPathSetting.Length > 0)
{
buttonDetectDevices.Enabled = true;
}
}
private string[] executeShellAndReturn(string command)
{
string adbPathSetting = Properties.Settings.Default.AdbPath;
string compoundCommand = String.Format("/c {0} {1}", adbPathSetting, command);
var processStartInfo = new ProcessStartInfo("cmd", compoundCommand)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process
{
StartInfo = processStartInfo
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
string[] response = new string[] { };
string[] lines = output.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
foreach (var line in lines)
{
if (!string.IsNullOrEmpty(line))
{
Array.Resize(ref response, response.Length + 1);
response[response.Length - 1] = line;
}
}
return response;
}
private async Task executeCopyCommand()
{
await Task.Run(() =>
{
string adbPathSetting = Properties.Settings.Default.AdbPath;
string outDirSetting = Properties.Settings.Default.OutDir;
string compoundCommand = String.Format("/c {0} pull -a -Z /sdcard/Telegram {1}", adbPathSetting, outDirSetting);
var processStartInfo = new ProcessStartInfo("cmd", compoundCommand)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process
{
StartInfo = processStartInfo
};
process.Start();
process.WaitForExit();
});
}
private void buttonCheckForTelegramData_Click(object sender, EventArgs e)
{
string[] output = executeShellAndReturn("shell ls /sdcard/Telegram");
if (output.Length == 0)
{
labelTelegramDataCheck.Text = "NO Telegram Data detected!";
} else
{
labelTelegramDataCheck.Text = "Data was found on the device!";
buttonCopyData.Enabled = true;
}
}
private void buttonSelectAdb_Click(object sender, EventArgs e)
{
if (openFileDialogAdbSelector.ShowDialog() == DialogResult.OK)
{
string selectedFile = openFileDialogAdbSelector.FileName;
// Sets the input field with the selected path.
textBoxAdbPath.Text = selectedFile;
// Save for later uses.
Properties.Settings.Default.AdbPath = selectedFile;
Properties.Settings.Default.Save();
}
}
private void buttonSelectOutDir_Click(object sender, EventArgs e)
{
if (folderBrowserDialogOutDirSelector.ShowDialog() == DialogResult.OK)
{
string selectedFolder = folderBrowserDialogOutDirSelector.SelectedPath;
// Sets the input field with the selected path.
textBoxOutDir.Text = selectedFolder;
// Save for later uses.
Properties.Settings.Default.OutDir = selectedFolder;
Properties.Settings.Default.Save();
}
}
private void buttonDetectDevices_Click(object sender, EventArgs e)
{
string[] output = executeShellAndReturn("devices");
if (output.Length == 0)
{
labelDetectedDevices.Text = "No devices detected!";
} else
{
string firstDevice = "";
foreach (var line in output)
{
if (!line.StartsWith("List of"))
{
Console.WriteLine(line);
firstDevice = line;
break;
}
}
firstDevice = String.Format("Device found: {0}", firstDevice);
int maxStringLength = 40;
if (maxStringLength > firstDevice.Length)
{
maxStringLength = firstDevice.Length;
}
labelDetectedDevices.Text = firstDevice.Substring(0, maxStringLength);
buttonCheckForTelegramData.Enabled = true;
}
}
private async void buttonCopyData_Click(object sender, EventArgs e)
{
progressBarCopy.Style = ProgressBarStyle.Marquee;
await executeCopyCommand();
progressBarCopy.Style = ProgressBarStyle.Blocks;
MessageBox.Show("Data was copied to your local machine.", "Operation finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}