PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/myseriesproject/actions/ApplicationActions.java

http://myseriesproject.googlecode.com/
Java | 437 lines | 392 code | 36 blank | 9 comment | 75 complexity | bbfc8ce7b8820224d7091a9157959f26 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package myseriesproject.actions;
  6. import com.googlecode.soptions.Option;
  7. import database.SeriesRecord;
  8. import help.About;
  9. import help.CheckUpdate;
  10. import help.ContactWay;
  11. import help.Help;
  12. import java.awt.Desktop;
  13. import java.io.File;
  14. import java.io.FileFilter;
  15. import java.io.FilenameFilter;
  16. import java.io.IOException;
  17. import java.lang.Integer;
  18. import java.net.URI;
  19. import java.sql.SQLException;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.Vector;
  23. import java.util.logging.Level;
  24. import javax.swing.JFrame;
  25. import javax.swing.JOptionPane;
  26. import javax.swing.JTabbedPane;
  27. import javax.swing.event.ChangeEvent;
  28. import javax.swing.table.TableColumnModel;
  29. import myComponents.MyMessages;
  30. import myseriesproject.MySeries;
  31. import myseriesproject.filters.Filters;
  32. import myseriesproject.series.RecycleSeries;
  33. import myseriesproject.series.Series;
  34. import tools.internetUpdate.tvrage.TrGetId;
  35. import myComponents.MyUsefulFunctions;
  36. import myComponents.myToolbar.ToolbarButton;
  37. import myComponents.myToolbar.ToolbarCustomize;
  38. import myseriesproject.MySeriesConstants;
  39. import sdialogs.Info;
  40. import tools.MySeriesLogger;
  41. import tools.misc.housekeeping.HouseKeeping;
  42. import tools.misc.latestNews.LatestNews;
  43. import tools.options.MySeriesOptions;
  44. import tools.options.OptionsPanel;
  45. import tools.options.Paths;
  46. /**
  47. *
  48. * @author ssoldatos
  49. */
  50. public class ApplicationActions {
  51. public static void showOptions(MySeries m) {
  52. MySeries.glassPane.activate(null);
  53. MySeriesLogger.logger.log(Level.INFO, "Showing options panel");
  54. OptionsPanel a = new OptionsPanel(m);
  55. }
  56. public static void exitApplication(MySeries m) {
  57. MySeriesLogger.logger.log(Level.INFO, "Performing exiting application actions");
  58. int divLocation = m.splitPane_main.getDividerLocation();
  59. int feedDivLocation = m.feedSplitPanel.getDividerLocation();
  60. String filter = m.combobox_filters.getSelectedItem() != null ? (String) m.combobox_filters.getSelectedItem() : "";
  61. ArrayList<Option> ops = new ArrayList<Option>();
  62. ops.add(new Option(MySeriesOptions.DIVIDER_LOCATION,Option.INTEGER_CLASS,divLocation));
  63. ops.add(new Option(MySeriesOptions.FEED_DIVIDER_LOCATION,Option.INTEGER_CLASS, feedDivLocation));
  64. ops.add(new Option(MySeriesOptions.ACTIVE_FILTER,Option.STRING_CLASS,filter));
  65. if (m.getExtendedState() != JFrame.ICONIFIED) {
  66. ops.add(new Option(MySeriesOptions.WINDOW_STATE,Option.INTEGER_CLASS, m.getExtendedState()));
  67. }
  68. ops.add(new Option(MySeriesOptions.WIDTH,Option.INTEGER_CLASS,m.getWidth()));
  69. ops.add(new Option(MySeriesOptions.HEIGHT,Option.INTEGER_CLASS,m.getHeight()));
  70. ops.add(new Option(MySeriesOptions.TOOLBAR_POSITION,Option.INTEGER_CLASS,m.getToolbarPosition()));
  71. Integer[] w = getTablesWidths(m);
  72. ops.add(new Option(MySeriesOptions.TABLE_WIDTHS,Option.ARRAY_CLASS,w));
  73. MySeries.options.saveOptions(ops);
  74. deleteJVMFiles();
  75. MySeriesLogger.logger.log(Level.INFO, "Application exiting...");
  76. System.exit(0);
  77. }
  78. private static void deleteJVMFiles() {
  79. MySeriesLogger.logger.log(Level.INFO, "Delete JVM log files");
  80. File[] logs = (new File(MySeriesOptions._USER_DIR_)).listFiles(new FileFilter() {
  81. @Override
  82. public boolean accept(File pathname) {
  83. if (pathname.getName().startsWith("hs_err")) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. });
  89. for (int i = 0; i < logs.length; i++) {
  90. File file = logs[i];
  91. file.delete();
  92. }
  93. MySeriesLogger.logger.log(Level.INFO, "JVM log files deleted");
  94. }
  95. private static Integer[] getTablesWidths(MySeries m) {
  96. ArrayList<Integer> widths = new ArrayList<Integer>();
  97. TableColumnModel ts = m.tableSeries.getColumnModel();
  98. for (int i = 0; i < ts.getColumnCount(); i++) {
  99. widths.add(ts.getColumn(i).getWidth());
  100. }
  101. ts = m.tableEpisodes.getColumnModel();
  102. for (int i = 0; i < ts.getColumnCount(); i++) {
  103. widths.add(ts.getColumn(i).getWidth());
  104. }
  105. ts = m.tableFilters.getColumnModel();
  106. for (int i = 0; i < ts.getColumnCount(); i++) {
  107. widths.add(ts.getColumn(i).getWidth());
  108. }
  109. Integer[] ar = new Integer[widths.size()];
  110. return widths.toArray(ar);
  111. }
  112. public static void about(MySeries m) {
  113. MySeries.glassPane.activate(null);
  114. MySeriesLogger.logger.log(Level.INFO, "Showing about panel");
  115. About a = new About(m);
  116. }
  117. public static void checkUpdates() {
  118. MySeries.glassPane.activate(null);
  119. MySeriesLogger.logger.log(Level.INFO, "Showing check update panel");
  120. new CheckUpdate(false);
  121. }
  122. public static void clearLogFiles() {
  123. MySeriesLogger.logger.log(Level.INFO, "Clearing log files");
  124. File dir = new File(MySeriesOptions._USER_DIR_);
  125. File[] files = dir.listFiles(new FilenameFilter() {
  126. @Override
  127. public boolean accept(File dir, String name) {
  128. if (name.matches("MySeriesLogs_([1-9]\\.html$|[0-9]\\.html\\.\\d+)")) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. });
  134. if (files.length == 0) {
  135. MySeriesLogger.logger.log(Level.INFO, "No old files");
  136. MyMessages.message("Delete log files", "There are no old log files to delete");
  137. return;
  138. }
  139. int ans = MyMessages.confirm("Delete log files",
  140. "Realy delete the following " + files.length + " log files:\n"
  141. + MyUsefulFunctions.listArray(null, true), true);
  142. if (ans == JOptionPane.YES_OPTION) {
  143. for (int i = 0; i < files.length; i++) {
  144. File file = files[i];
  145. if (file.delete()) {
  146. MySeriesLogger.logger.log(Level.FINE, "Deleted file {0}", file.getName());
  147. }
  148. }
  149. }
  150. }
  151. public static void viewLog(MySeries m) {
  152. Desktop d = Desktop.getDesktop();
  153. if (!Desktop.isDesktopSupported()) {
  154. MySeriesLogger.logger.log(Level.SEVERE, "Desktop is not supported");
  155. MyMessages.error("Sorry!!!", "Your OS does not support this function", true);
  156. } else {
  157. if (!d.isSupported(Desktop.Action.OPEN)) {
  158. MySeriesLogger.logger.log(Level.SEVERE, "Open file is not supported");
  159. MyMessages.error("Sorry!!!", "Your OS does not support this function", true);
  160. } else {
  161. try {
  162. d.open(new File(Paths.LOGS_PATH + "MySeriesLog_0.html"));
  163. MySeriesLogger.logger.log(Level.FINE, "Log file opened for viewing");
  164. } catch (Exception ex) {
  165. MySeriesLogger.logger.log(Level.SEVERE, "Could not read the log file", ex);
  166. MyMessages.error("View Log", "Couldn't find the log file", true);
  167. }
  168. }
  169. }
  170. }
  171. public static void changeTab(MySeries m, ChangeEvent evt) {
  172. Vector<SeriesRecord> series = null;
  173. JTabbedPane tabs = (JTabbedPane) evt.getSource();
  174. if (tabs.getComponentCount() == 0 || tabs.getSelectedComponent() == null) {
  175. return;
  176. }
  177. int name = Integer.parseInt(tabs.getSelectedComponent().getName());
  178. MySeriesLogger.logger.log(Level.INFO, "Changing tab to {0}", name);
  179. try {
  180. if (name == MySeries.TAB_EPISODES_ID) {
  181. } else if (name == MySeries.TAB_FILTERS_ID) {
  182. Filters.getFilteredSeries(m.comboBox_seen, m.comboBox_filterSubtitles, m.combobox_downloaded,m.combobox_filters, m.tableFilters);
  183. } else if (name == MySeries.TAB_RATINGS_ID) {
  184. m.statSeries.refresh(MySeries.options.getBooleanOption(MySeriesOptions.UNIFIED_SERIES));
  185. m.statEpisodes.refresh();
  186. } else if (name == MySeries.TAB_SCHEDULE_ID) {
  187. //MySeries.scheduler.refreshCalendar(MySeries.scheduler.getRealMonth(),MySeries.scheduler.getRealYear());
  188. }
  189. } catch (SQLException ex) {
  190. MySeriesLogger.logger.log(Level.SEVERE, "Sql exception occured", ex);
  191. }
  192. }
  193. public static void downloadScreenshot() {
  194. SeriesRecord series = Series.getCurrentSerial();
  195. MySeriesLogger.logger.log(Level.INFO, "Downloading screenshot for series {0}", series.getFullTitle());
  196. if (series.getTvrage_ID() == 0) {
  197. TrGetId tr = new TrGetId(null, series.getSeries_ID(), series.getFullTitle(), true);
  198. }
  199. }
  200. public static void recycleSeries(MySeries m) {
  201. try {
  202. MySeriesLogger.logger.log(Level.INFO, "Restoring series");
  203. ArrayList<SeriesRecord> series = Series.getSeries(true);
  204. if (series.isEmpty()) {
  205. MySeriesLogger.logger.log(Level.INFO, "No deleted series in recycle bin");
  206. MyMessages.message("Recycle bin", "There are no deleted series in the recycle bin");
  207. } else {
  208. MySeriesLogger.logger.log(Level.INFO, "Opening recycling series panel");
  209. new RecycleSeries(series, m);
  210. }
  211. } catch (SQLException ex) {
  212. MySeriesLogger.logger.log(Level.SEVERE, "Sql exception occured", ex);
  213. }
  214. }
  215. public static void showHelp(MySeries m) {
  216. if (!MySeries.isHelp) {
  217. MySeriesLogger.logger.log(Level.INFO, "Showing help panel");
  218. new Help(m);
  219. }
  220. }
  221. public static void deleteTorrents() {
  222. MySeriesLogger.logger.log(Level.INFO, "Deleting torrents");
  223. if (MyMessages.confirm("Delete torrents", "Do you really want to clear the torrents directory?", true) == JOptionPane.OK_OPTION) {
  224. File dir = new File(MySeriesOptions._USER_DIR_ + Paths.TORRENTS_PATH);
  225. if (dir.isDirectory()) {
  226. File[] torrents = dir.listFiles();
  227. int del = 0, notDel = 0;
  228. if (torrents.length == 0) {
  229. MySeriesLogger.logger.log(Level.INFO, "No torrents to delete");
  230. MyMessages.message("Delete torrents", "There are no torrents to delete");
  231. return;
  232. }
  233. for (int i = 0; i < torrents.length; i++) {
  234. File tor = torrents[i];
  235. MySeriesLogger.logger.log(Level.INFO, "Deleting torrent {0}", tor.getName());
  236. if (tor.delete()) {
  237. del++;
  238. MySeriesLogger.logger.log(Level.FINE, "Torrent {0} deleted", tor.getName());
  239. } else {
  240. notDel++;
  241. MySeriesLogger.logger.log(Level.WARNING, "Torrent {0} could not be deleted", tor.getName());
  242. }
  243. }
  244. String mess = del + " torrents deleted";
  245. if (notDel > 0) {
  246. mess += "and " + notDel + " torrents could not be deleted.";
  247. } else {
  248. mess += ".";
  249. }
  250. MySeriesLogger.logger.log(Level.FINE, mess);
  251. MyMessages.message("Delete torrents", mess);
  252. } else {
  253. MySeriesLogger.logger.log(Level.SEVERE, "The torrents directory does not exist");
  254. MyMessages.error("Delete torrents", "The torrents directory does not exist", true);
  255. }
  256. }
  257. }
  258. public static void warnForLogLevel() {
  259. MySeriesLogger.logger.log(Level.INFO, "Warn for log using");
  260. if (MySeries.options.getStringOption(MySeriesOptions.DEBUG_MODE).equals("ALL")) {
  261. if (MySeries.options.getBooleanOption(MySeriesOptions.WARN_FOR_LOG_USE)) {
  262. int a = MyMessages.confirm("Logging level", "Logging level is set to ALL.\n"
  263. + "That's for debugging reasons and may slow down the application.\n"
  264. + "Do you want to turn logging off?", true);
  265. if (a == JOptionPane.OK_OPTION) {
  266. ArrayList<Option> ops = new ArrayList<Option>();
  267. ops.add(new Option(MySeriesOptions.DEBUG_MODE,Option.STRING_CLASS, "OFF"));
  268. ops.add(new Option(MySeriesOptions.WARN_FOR_LOG_USE,Option.BOOLEAN_CLASS, false));
  269. MySeries.options.saveOptions(ops);
  270. }
  271. }
  272. MyMessages.logToPanel(Info.WARNING_MESS, "Logging level", "Logging level is set to ALL\n"
  273. + "That's for debugging reasons and may slow down the application\n");
  274. }
  275. }
  276. public static void houseKeeping() {
  277. MySeriesLogger.logger.log(Level.INFO, "Housekeeping action");
  278. HouseKeeping h = new HouseKeeping();
  279. }
  280. public static void customizeToolbar(MySeries m) {
  281. ToolbarCustomize tc = new ToolbarCustomize(m.myToolbar.getVisibleButtons());
  282. m.myToolbar.visibleButtons = tc.newVisibleButtons;
  283. MySeries.options.setOption(new Option(MySeriesOptions.TOOLBAR_BUTTONS,Option.ARRAY_CLASS, m.myToolbar.visibleButtons));
  284. m.myToolbar.populateToolbar();
  285. MyUsefulFunctions.createMemoryCons(m);
  286. }
  287. public static void googleCodePage() {
  288. MyUsefulFunctions.browse(MySeries.GOOGLE_CODE_URL);
  289. }
  290. public static void contactByMail() {
  291. MySeriesLogger.logger.log(Level.INFO, "Contacting by email");
  292. try {
  293. String subject = ("?subject=MySeries v." + MySeries.version).replaceAll("\\s", "%20");
  294. URI email = new URI("mailto:" + MySeries.EMAIL + subject);
  295. MyUsefulFunctions.sendMail(email);
  296. } catch (Exception ex) {
  297. MyMessages.warning("Sending mail", "Could not open your email client.\n"
  298. + "Please send it manually to " + MySeries.EMAIL, true);
  299. MySeriesLogger.logger.log(Level.WARNING, "Could not send mail", ex);
  300. }
  301. }
  302. public static void paypalDonation() {
  303. MySeriesLogger.logger.log(Level.INFO, "Showing donation message");
  304. int a = MyMessages.confirm("Paypal Donation", "Thanks for your interest in donating for MySeries developement."
  305. + "Even 1$ or 1รข&#x201A;? is a real help.You can make your donation via paypal to " + MySeries.EMAIL + ".\nClick OK"
  306. + " to go to the donation page", true);
  307. if (a == JOptionPane.YES_OPTION) {
  308. MySeriesLogger.logger.log(Level.INFO, "Go to donation page");
  309. MyUsefulFunctions.browse(MySeries.DONATION_URL);
  310. }
  311. }
  312. public static void chooseContactWay() {
  313. MySeriesLogger.logger.log(Level.INFO, "Choosing contact way");
  314. ContactWay cw = new ContactWay();
  315. if (cw.way == ContactWay.EMAIL) {
  316. contactByMail();
  317. } else if (cw.way == ContactWay.GOOGLE_CODE) {
  318. contactByGoogleCode();
  319. } else {
  320. }
  321. }
  322. private static void contactByGoogleCode() {
  323. MySeriesLogger.logger.log(Level.INFO, "Going to google code report");
  324. MyUsefulFunctions.browse(MySeries.GOOGLE_CODE_TRACKER);
  325. }
  326. public static void warnForJREVersion() {
  327. MySeriesLogger.logger.log(Level.INFO, "Checking java version");
  328. String version = System.getProperty("java.runtime.version");
  329. MySeriesLogger.logger.log(Level.INFO, "Java version is {0}", version);
  330. if (version != null && version.startsWith("1.7")) {
  331. if (MySeries.options.getBooleanOption(MySeriesOptions.WARN_FOR_VERSION)) {
  332. MyMessages.warning("Java version " + version, "The application is written for java 6.\n"
  333. + "It seems that you are using java 7.\nThere might be some incopatibility issues",
  334. true);
  335. MySeries.options.setOption(new Option(MySeriesOptions.WARN_FOR_VERSION,Option.BOOLEAN_CLASS, false),true);
  336. } else {
  337. MyMessages.logToPanel(Info.WARNING_MESS,"Java version", "The application is written for java 6.\n"
  338. + "It seems that you are using java 7.\nThere might be some incopatibility issues");
  339. }
  340. }
  341. }
  342. public static void restartApplication(MySeries m) {
  343. String os = System.getProperty("os.name").toLowerCase();
  344. if (os.startsWith("windows")) {
  345. restartWindows(m);
  346. } else if (os.startsWith("linux")) {
  347. restartLinux(m);
  348. } else {
  349. restartDefault(m);
  350. }
  351. }
  352. private static void restartWindows(MySeries m) {
  353. File exec = new File(MySeriesConstants.WINDOWS_EXECUTABLE);
  354. restart(m, exec);
  355. }
  356. private static void restartLinux(MySeries m) {
  357. File exec = new File(MySeriesConstants.LINUX_EXECUTABLE);
  358. restart(m, exec);
  359. }
  360. private static void restartDefault(MySeries m) {
  361. File exec = new File(MySeriesConstants.APPLICATION_JAR);
  362. restartDefault(m, exec);
  363. }
  364. private static void restart(MySeries m, File exec) {
  365. try {
  366. String pathToApp = exec.getAbsolutePath();
  367. File startingDir = exec.getAbsoluteFile().getParentFile();
  368. if (exec.exists()) {
  369. MyUsefulFunctions.runExternalProgram(new String[]{pathToApp}, startingDir);
  370. ApplicationActions.exitApplication(m);
  371. } else {
  372. MySeriesLogger.logger.log(Level.SEVERE, "Could not restart application");
  373. MyMessages.error("Restart Application", "Could not restart the application", false);
  374. }
  375. } catch (IOException ex) {
  376. MySeriesLogger.logger.log(Level.SEVERE, "Could not restart application", ex);
  377. MyMessages.error("Restart Application", "Could not restart the application", false);
  378. }
  379. }
  380. private static void restartDefault(MySeries m, File exec) {
  381. try {
  382. String pathToApp = exec.getAbsolutePath();
  383. File startingDir = exec.getAbsoluteFile().getParentFile();
  384. if (exec.exists()) {
  385. MyUsefulFunctions.runExternalProgram(new String[]{"java", "-jar", pathToApp}, startingDir);
  386. ApplicationActions.exitApplication(m);
  387. } else {
  388. MySeriesLogger.logger.log(Level.SEVERE, "Could not restart application");
  389. MyMessages.error("Restart Application", "Could not restart the application", false);
  390. }
  391. } catch (IOException ex) {
  392. MySeriesLogger.logger.log(Level.SEVERE, "Could not restart application", ex);
  393. MyMessages.error("Restart Application", "Could not restart the application", false);
  394. }
  395. }
  396. public static void latestNews(MySeries m,boolean start) {
  397. LatestNews news = new LatestNews(m,start);
  398. }
  399. private ApplicationActions() {
  400. }
  401. }