Beginnings of a Swing GUI.

This commit is contained in:
Fabian Schlenz 2016-07-02 14:12:57 +02:00
parent c0bc3cdd2d
commit ba7e76087a
6 changed files with 192 additions and 56 deletions

View File

@ -5,24 +5,18 @@ import com.github.badoualy.telegram.api.TelegramApp;
import com.github.badoualy.telegram.api.TelegramClient;
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
import de.fabianonline.telegram_backup.Config;
import de.fabianonline.telegram_backup.ApiStorage;
import de.fabianonline.telegram_backup.UserManager;
import de.fabianonline.telegram_backup.DownloadManager;
import de.fabianonline.telegram_backup.CommandLineDownloadProgress;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
public class CommandLineController {
private ApiStorage storage;
private CommandLineOptions options;
public TelegramApp app;
public UserManager user = null;
public CommandLineController(String[] args) {
options = new CommandLineOptions(args);
public CommandLineController(CommandLineOptions options) {
if (options.cmd_help) this.show_help();
if (options.cmd_list_accounts) this.list_accounts();
if (options.account==null && !options.cmd_login) {
@ -95,17 +89,12 @@ public class CommandLineController {
private void list_accounts() {
System.out.println("List of available accounts:");
int count = 0;
File folder = new File(Config.FILE_BASE);
File[] files = folder.listFiles();
for (File f : files) {
if (f.isDirectory()) {
count++;
System.out.println(" " + f.getName());
List<String> accounts = Utils.getAccounts();
if (accounts.size()>0) {
for (String str : accounts) {
System.out.println(" " + str);
}
}
if (count>0) {
System.out.println("Use '--acount <x>' to use one of those accounts.");
System.out.println("Use '--account <x>' to use one of those accounts.");
} else {
System.out.println("NO ACCOUNTS FOUND");
System.out.println("Use '--login' to login to a telegram account.");
@ -118,40 +107,4 @@ public class CommandLineController {
System.exit(1);
}
private class CommandLineOptions {
public String account = null;
public boolean cmd_help = false;
public boolean cmd_login = false;
public boolean cmd_debug = false;
public boolean cmd_list_accounts = false;
public Integer limit_messages = null;
public CommandLineOptions(String[] args) {
String last_cmd = null;
for (String arg : args) {
if (last_cmd != null) {
switch(last_cmd) {
case "--account": this.account=arg; break;
case "--limit-messages": this.limit_messages=Integer.parseInt(arg); break;
}
last_cmd = null;
continue;
}
switch(arg) {
case "--account": last_cmd=arg; continue;
case "--help": this.cmd_help=true; break;
case "--login": this.cmd_login=true; break;
case "--debug": this.cmd_debug=true; break;
case "--list-accounts": this.cmd_list_accounts=true; break;
case "--limit-messages": last_cmd=arg; continue;
default: throw new RuntimeException("Unknown command " + arg);
}
}
if (last_cmd != null) {
CommandLineController.show_error("Command " + last_cmd + " had no parameter set.");
}
}
}
}

View File

@ -0,0 +1,59 @@
package de.fabianonline.telegram_backup;
class CommandLineOptions {
public boolean cmd_console = false;
public String account = null;
public boolean cmd_help = false;
public boolean cmd_login = false;
public boolean cmd_debug = false;
public boolean cmd_list_accounts = false;
public Integer limit_messages = null;
public CommandLineOptions(String[] args) {
String last_cmd = null;
for (String arg : args) {
if (last_cmd != null) {
switch (last_cmd) {
case "--account":
this.account = arg;
break;
case "--limit-messages":
this.limit_messages = Integer.parseInt(arg);
break;
}
last_cmd = null;
continue;
}
switch (arg) {
case "--account":
last_cmd = arg;
continue;
case "--help":
this.cmd_help = true;
break;
case "--login":
this.cmd_login = true;
break;
case "--debug":
this.cmd_debug = true;
break;
case "--list-accounts":
this.cmd_list_accounts = true;
break;
case "--limit-messages":
last_cmd = arg;
continue;
case "--console":
this.cmd_console = true;
break;
default:
throw new RuntimeException("Unknown command " + arg);
}
}
if (last_cmd != null) {
CommandLineController.show_error("Command " + last_cmd + " had no parameter set.");
}
}
}

View File

@ -4,6 +4,12 @@ import de.fabianonline.telegram_backup.CommandLineController;
public class CommandLineRunner {
public static void main(String[] args) {
new CommandLineController(args);
CommandLineOptions options = new CommandLineOptions(args);
if (true || options.cmd_console) {
// Always use the console for now.
new CommandLineController(options);
} else {
new GUIController(options);
}
}
}

View File

@ -0,0 +1,88 @@
package de.fabianonline.telegram_backup;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class GUIController {
private CommandLineOptions options;
public GUIController(CommandLineOptions options) {
this.options = options;
showAccountChooserDialog();
}
private void showAccountChooserDialog() {
JDialog accountChooser = new JDialog();
accountChooser.setTitle("Choose account");
accountChooser.setSize(400, 200);
JPanel vert = new JPanel();
vert.setLayout(new BorderLayout());
vert.add(new JLabel("Please select the account to use or create a new one."), BorderLayout.NORTH);
Vector<String> accounts = Utils.getAccounts();
JList<String> list = new JList<String>(accounts);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
vert.add(list, BorderLayout.CENTER);
JPanel bottom = new JPanel(new GridLayout(1, 2));
JButton btnAddAccount = new JButton("Add account");
bottom.add(btnAddAccount);
JButton btnLogin = new JButton("Login");
btnLogin.setEnabled(false);
bottom.add(btnLogin);
vert.add(bottom, BorderLayout.SOUTH);
accountChooser.add(vert);
accountChooser.setVisible(true);
accountChooser.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
btnLogin.setEnabled(true);
}
});
btnAddAccount.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
accountChooser.setVisible(false);
accountChooser.dispose();
addAccountDialog();
}
});
}
private void addAccountDialog() {
JDialog loginDialog = new JDialog();
loginDialog.setTitle("Add an account");
loginDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel sections = new JPanel();
sections.setLayout(new BoxLayout(sections, BoxLayout.Y_AXIS));
JPanel top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
top.add(new JLabel("Please enter your phone number in international format:"));
top.add(new JTextField("+49123773212"));
sections.add(top);
sections.add(Box.createVerticalStrut(5));
sections.add(new JSeparator(SwingConstants.HORIZONTAL));
JPanel middle = new JPanel();
middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS));
middle.add(new JLabel("Telegram sent you a code. Enter it here:"));
middle.add(new JTextField());
middle.setEnabled(false);
sections.add(middle);
sections.add(Box.createVerticalStrut(5));
sections.add(new JSeparator(SwingConstants.HORIZONTAL));
loginDialog.add(sections);
loginDialog.setVisible(true);
}
}

View File

@ -0,0 +1,19 @@
package de.fabianonline.telegram_backup;
import java.io.File;
import java.util.List;
import java.util.Vector;
public class Utils {
static Vector<String> getAccounts() {
Vector<String> accounts = new Vector<String>();
File folder = new File(Config.FILE_BASE);
File[] files = folder.listFiles();
for (File f : files) {
if (f.isDirectory() && f.getName().startsWith("+")) {
accounts.add(f.getName());
}
}
return accounts;
}
}

11
src/main/main.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>