From a2224b326d19d80251351565179d6e5a320c1408 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 22 Feb 2017 06:53:51 +0100 Subject: [PATCH 1/4] Database is now a singleton. --- .../java/de/fabianonline/telegram_backup/Database.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/de/fabianonline/telegram_backup/Database.java b/src/main/java/de/fabianonline/telegram_backup/Database.java index 9641ffa..9b945b6 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Database.java +++ b/src/main/java/de/fabianonline/telegram_backup/Database.java @@ -53,6 +53,7 @@ public class Database { public UserManager user_manager; public TelegramClient client; private final static Logger logger = LoggerFactory.getLogger(Database.class); + private static Database instance = null; public Database(UserManager user_manager, TelegramClient client) { this(user_manager, client, true); @@ -80,6 +81,7 @@ public class Database { } this.init(update_db); + instance = this; System.out.println("Database is ready."); } @@ -89,6 +91,11 @@ public class Database { updates.doUpdates(); } + public static Database getInstance() { + if (instance == null) throw new RuntimeException("Database is not initialized but getInstance() was called."); + return instance; + } + public void backupDatabase(int currentVersion) { String filename = String.format(Config.FILE_NAME_DB_BACKUP, currentVersion); System.out.println(" Creating a backup of your database as " + filename); From 42112d7607c8f69e1a4b84c7fb2f6042c69eda4a Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 22 Feb 2017 06:54:01 +0100 Subject: [PATCH 2/4] Updated the year in the version string. --- .../de/fabianonline/telegram_backup/CommandLineController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index 591ee2c..d30481b 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -153,7 +153,7 @@ public class CommandLineController { } private void printHeader() { - System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz"); + System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016, 2017 Fabian Schlenz"); System.out.println(); System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are"); System.out.println("welcome to redistribute it under certain conditions; run it with '--license' for details."); From 6d772a3be15f5cd003811bcc012cc444bdf3454a Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 22 Feb 2017 06:57:58 +0100 Subject: [PATCH 3/4] UserManager is now a Singleton as well. --- .../CommandLineController.java | 15 ++++++++------ .../telegram_backup/Database.java | 20 ++++++++----------- .../telegram_backup/DownloadManager.java | 6 +++--- .../TelegramUpdateHandler.java | 2 +- .../telegram_backup/TestFeatures.java | 2 +- .../telegram_backup/UserManager.java | 12 ++++++++++- .../exporter/HTMLExporter.java | 5 +++-- 7 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index d30481b..fbf6bb3 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -37,7 +37,6 @@ public class CommandLineController { private static Logger logger = LoggerFactory.getLogger(CommandLineController.class); private ApiStorage storage; public TelegramApp app; - public UserManager user = null; public CommandLineController() { logger.info("CommandLineController started. App version {}", Config.APP_APPVER); @@ -76,8 +75,11 @@ public class CommandLineController { TelegramClient client = Kotlogram.getDefaultClient(app, storage, Kotlogram.PROD_DC4, handler); try { - logger.info("Creating UserManager"); - user = new UserManager(client); + logger.info("Initializing UserManager"); + UserManager.init(client); + Database.init(client); + + UserManager user = UserManager.getInstance(); if (!CommandLineOptions.cmd_login && !user.isLoggedIn()) { System.out.println("Your authorization data is invalid or missing. You will have to login with Telegram again."); @@ -104,7 +106,7 @@ public class CommandLineController { logger.debug("CommandLineOptions.val_export: {}", CommandLineOptions.val_export); if (CommandLineOptions.val_export != null) { if (CommandLineOptions.val_export.toLowerCase().equals("html")) { - (new HTMLExporter()).export(user); + (new HTMLExporter()).export(); System.exit(0); } else { show_error("Unknown export format."); @@ -125,7 +127,7 @@ public class CommandLineController { } logger.info("Initializing Download Manager"); - DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress()); + DownloadManager d = new DownloadManager(client, new CommandLineDownloadProgress()); logger.debug("Calling DownloadManager.downloadMessages with limit {}", CommandLineOptions.val_limit_messages); d.downloadMessages(CommandLineOptions.val_limit_messages); @@ -141,7 +143,7 @@ public class CommandLineController { logger.error("Exception caught!", e); } finally { if (CommandLineOptions.cmd_daemon) { - handler.setUser(user, client); + handler.activate(); System.out.println("DAEMON mode requested - keeping running."); } else { client.close(); @@ -209,6 +211,7 @@ public class CommandLineController { } private void cmd_login(String phone) throws RpcErrorException, IOException { + UserManager user = UserManager.getInstance(); if (phone==null) { System.out.println("Please enter your phone number in international format."); System.out.println("Example: +4917077651234"); diff --git a/src/main/java/de/fabianonline/telegram_backup/Database.java b/src/main/java/de/fabianonline/telegram_backup/Database.java index 9b945b6..46f1267 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Database.java +++ b/src/main/java/de/fabianonline/telegram_backup/Database.java @@ -55,12 +55,8 @@ public class Database { private final static Logger logger = LoggerFactory.getLogger(Database.class); private static Database instance = null; - public Database(UserManager user_manager, TelegramClient client) { - this(user_manager, client, true); - } - - public Database(UserManager user_manager, TelegramClient client, boolean update_db) { - this.user_manager = user_manager; + private Database(TelegramClient client) { + this.user_manager = UserManager.getInstance(); this.client = client; System.out.println("Opening database..."); try { @@ -80,15 +76,15 @@ public class Database { CommandLineController.show_error("Could not connect to SQLITE database."); } - this.init(update_db); - instance = this; + // Run updates + DatabaseUpdates updates = new DatabaseUpdates(conn, this); + updates.doUpdates(); + System.out.println("Database is ready."); } - private void init(boolean update_db) { - if (!update_db) return; - DatabaseUpdates updates = new DatabaseUpdates(conn, this); - updates.doUpdates(); + public static void init(TelegramClient c) { + instance = new Database(c); } public static Database getInstance() { diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index da4fe89..c448e48 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -60,11 +60,11 @@ public class DownloadManager { static boolean last_download_succeeded = true; static final Logger logger = LoggerFactory.getLogger(DownloadManager.class); - public DownloadManager(UserManager u, TelegramClient c, DownloadProgressInterface p) { - this.user = u; + public DownloadManager(TelegramClient c, DownloadProgressInterface p) { + this.user = UserManager.getInstance(); this.client = c; this.prog = p; - this.db = new Database(u, c); + this.db = Database.getInstance(); } public void downloadMessages(Integer limit) throws RpcErrorException, IOException { diff --git a/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java b/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java index 501a10e..31add06 100644 --- a/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java +++ b/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java @@ -32,7 +32,7 @@ class TelegramUpdateHandler implements UpdateCallback { private Database db = null; public boolean debug = false; - public void setUser(UserManager user, TelegramClient client) { this.user = user; this.db = new Database(user, client, false);} + public void activate() { this.user = UserManager.getInstance(); this.db = Database.getInstance();} public void onUpdates(TelegramClient c, TLUpdates u) { if (db==null) return; diff --git a/src/main/java/de/fabianonline/telegram_backup/TestFeatures.java b/src/main/java/de/fabianonline/telegram_backup/TestFeatures.java index 3b31d5f..64b678b 100644 --- a/src/main/java/de/fabianonline/telegram_backup/TestFeatures.java +++ b/src/main/java/de/fabianonline/telegram_backup/TestFeatures.java @@ -58,7 +58,7 @@ class TestFeatures { // Prints system.encoding and default charset System.out.println("Default Charset: " + Charset.defaultCharset()); System.out.println("file.encoding: " + System.getProperty("file.encoding")); - Database db = new Database(user, client, false); + Database db = Database.getInstance(); System.out.println("Database encoding: " + db.getEncoding()); } } diff --git a/src/main/java/de/fabianonline/telegram_backup/UserManager.java b/src/main/java/de/fabianonline/telegram_backup/UserManager.java index cca023c..41be834 100644 --- a/src/main/java/de/fabianonline/telegram_backup/UserManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/UserManager.java @@ -43,8 +43,13 @@ public class UserManager { private TLAuthorization auth = null; private boolean password_needed = false; private static Logger logger = LoggerFactory.getLogger(UserManager.class); + private static UserManager instance = null; - public UserManager(TelegramClient c) throws IOException { + public static void init(TelegramClient c) throws IOException { + instance = new UserManager(c); + } + + private UserManager(TelegramClient c) throws IOException { this.client = c; logger.debug("Calling getFullUser"); try { @@ -56,6 +61,11 @@ public class UserManager { } } + public static UserManager getInstance() { + if (instance==null) throw new RuntimeException("UserManager is not yet initialized."); + return instance; + } + public boolean isLoggedIn() { return user!=null; } public void sendCodeToPhoneNumber(String number) throws RpcErrorException, IOException { diff --git a/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java b/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java index b7267c8..1935fef 100644 --- a/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java +++ b/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java @@ -45,9 +45,10 @@ import org.slf4j.LoggerFactory; public class HTMLExporter { private static Logger logger = LoggerFactory.getLogger(HTMLExporter.class); - public void export(UserManager user) throws IOException { + public void export() throws IOException { try { - Database db = new Database(user, null); + UserManager user = UserManager.getInstance(); + Database db = Database.getInstance(); // Create base dir logger.debug("Creating base dir"); From c945f05c3d64da19c2494b71d1e694f05bebc794 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 22 Feb 2017 18:37:28 +0100 Subject: [PATCH 4/4] Added '--stats'. --- .../CommandLineController.java | 20 +++++++++++++++++++ .../telegram_backup/CommandLineOptions.java | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index fbf6bb3..61f101f 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -29,6 +29,8 @@ import java.io.IOException; import java.util.List; import java.util.Scanner; import java.util.Vector; +import java.util.HashMap; +import java.util.Map; import org.slf4j.LoggerFactory; import org.slf4j.Logger; @@ -92,6 +94,11 @@ public class CommandLineController { } } + if (CommandLineOptions.cmd_stats) { + cmd_stats(); + System.exit(0); + } + if (CommandLineOptions.val_test != null) { if (CommandLineOptions.val_test == 1) { TestFeatures.test1(); @@ -210,6 +217,18 @@ public class CommandLineController { return account; } + private void cmd_stats() { + HashMap map = new HashMap(); + map.put("count.accounts", Utils.getAccounts().size()); + map.put("count.messages", Database.getInstance().getMessageCount()); + map.put("messages.top_id", Database.getInstance().getTopMessageID()); + for(Map.Entry pair : Database.getInstance().getMessageMediaTypesWithCount().entrySet()) { + map.put(pair.getKey(), pair.getValue()); + } + + System.out.println(map.toString()); + } + private void cmd_login(String phone) throws RpcErrorException, IOException { UserManager user = UserManager.getInstance(); if (phone==null) { @@ -267,6 +286,7 @@ public class CommandLineController { System.out.println(" --license Displays the license of this program."); System.out.println(" -d, --daemon Keep running and automatically save new messages."); System.out.println(" --anonymize (Try to) Remove all sensitive information from output. Useful for requesting support."); + System.out.println(" --stats Print some usage statistics."); } private void list_accounts() { diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java index ac653b1..bf217c4 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java @@ -29,6 +29,7 @@ class CommandLineOptions { public static boolean cmd_daemon = false; public static boolean cmd_no_media = false; public static boolean cmd_anonymize = false; + public static boolean cmd_stats = false; public static String val_account = null; public static Integer val_limit_messages = null; @@ -112,6 +113,9 @@ class CommandLineOptions { case "--anonymize": cmd_anonymize = true; break; + + case "--stats": + cmd_stats = true; break; default: throw new RuntimeException("Unknown command " + arg);