From ba7e76087a65c4fae6809aec99ae242fed5260d1 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Sat, 2 Jul 2016 14:12:57 +0200 Subject: [PATCH] Beginnings of a Swing GUI. --- .../CommandLineController.java | 63 ++----------- .../telegram_backup/CommandLineOptions.java | 59 +++++++++++++ .../telegram_backup/CommandLineRunner.java | 8 +- .../telegram_backup/GUIController.java | 88 +++++++++++++++++++ .../fabianonline/telegram_backup/Utils.java | 19 ++++ src/main/main.iml | 11 +++ 6 files changed, 192 insertions(+), 56 deletions(-) create mode 100644 src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java create mode 100644 src/main/java/de/fabianonline/telegram_backup/GUIController.java create mode 100644 src/main/java/de/fabianonline/telegram_backup/Utils.java create mode 100644 src/main/main.iml diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index cabffb7..3431744 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -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 accounts = Utils.getAccounts(); + if (accounts.size()>0) { + for (String str : accounts) { + System.out.println(" " + str); } - } - if (count>0) { - System.out.println("Use '--acount ' to use one of those accounts."); + System.out.println("Use '--account ' 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."); - } - } - } } diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java new file mode 100644 index 0000000..dc2de3f --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java @@ -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."); + } + } +} diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java index d57ce0f..247e3f0 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java @@ -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); + } } } diff --git a/src/main/java/de/fabianonline/telegram_backup/GUIController.java b/src/main/java/de/fabianonline/telegram_backup/GUIController.java new file mode 100644 index 0000000..98e4f93 --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/GUIController.java @@ -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 accounts = Utils.getAccounts(); + JList list = new JList(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); + } +} diff --git a/src/main/java/de/fabianonline/telegram_backup/Utils.java b/src/main/java/de/fabianonline/telegram_backup/Utils.java new file mode 100644 index 0000000..69fc5b6 --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/Utils.java @@ -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 getAccounts() { + Vector accounts = new Vector(); + 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; + } +} diff --git a/src/main/main.iml b/src/main/main.iml new file mode 100644 index 0000000..908ad4f --- /dev/null +++ b/src/main/main.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file