Search in sources :

Example 1 with ConfirmListener

use of org.jivesoftware.spark.component.ConfirmDialog.ConfirmListener in project Spark by igniterealtime.

the class CheckUpdates method promptForInstallation.

/**
 * Prompts the user to install the latest Spark.
 *
 * @param downloadedFile the location of the latest downloaded client.
 * @param title          the title
 * @param message        the message
 */
private void promptForInstallation(final File downloadedFile, String title, String message) {
    ConfirmDialog confirm = new ConfirmDialog();
    confirm.showConfirmDialog(SparkManager.getMainWindow(), title, message, Res.getString("yes"), Res.getString("no"), null);
    confirm.setConfirmListener(new ConfirmListener() {

        public void yesOption() {
            try {
                if (Spark.isWindows()) {
                    Runtime.getRuntime().exec(downloadedFile.getAbsolutePath());
                } else if (Spark.isMac()) {
                    Runtime.getRuntime().exec("open " + downloadedFile.getCanonicalPath());
                }
            } catch (IOException e) {
                Log.error(e);
            }
            SparkManager.getMainWindow().shutdown();
        }

        public void noOption() {
        }
    });
}
Also used : IOException(java.io.IOException) ConfirmListener(org.jivesoftware.spark.component.ConfirmDialog.ConfirmListener) ConfirmDialog(org.jivesoftware.spark.component.ConfirmDialog)

Example 2 with ConfirmListener

use of org.jivesoftware.spark.component.ConfirmDialog.ConfirmListener in project Spark by igniterealtime.

the class CheckUpdates method checkForUpdate.

/**
 * Checks Spark Manager and/or Jive Software for the latest version of Spark.
 *
 * @param explicit true if the user explicitly asks for the latest version.
 * @throws Exception if there is an error during check
 */
public void checkForUpdate(boolean explicit) throws Exception {
    if (UPDATING) {
        return;
    }
    UPDATING = true;
    if (isLocalBuildAvailable()) {
        return;
    }
    LocalPreferences localPreferences = SettingsManager.getLocalPreferences();
    // defaults to 7, 0=disabled
    int CheckForUpdates = localPreferences.getCheckForUpdates();
    if (CheckForUpdates == 0) {
        return;
    }
    Date lastChecked = localPreferences.getLastCheckForUpdates();
    if (lastChecked == null) {
        lastChecked = new Date();
        // This is the first invocation of Communicator
        localPreferences.setLastCheckForUpdates(lastChecked);
        SettingsManager.saveSettings();
    }
    // Check to see if it has been a CheckForUpdates (default 7) days
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(lastChecked);
    calendar.add(Calendar.DATE, CheckForUpdates);
    final Date lastCheckedPlusAPeriod = calendar.getTime();
    boolean periodOrLonger = new Date().getTime() >= lastCheckedPlusAPeriod.getTime();
    if (periodOrLonger || explicit || sparkPluginInstalled) {
        if (!explicit && !localPreferences.isBetaCheckingEnabled()) {
            return;
        }
        // Check version on server.
        lastChecked = new Date();
        localPreferences.setLastCheckForUpdates(lastChecked);
        SettingsManager.saveSettings();
        final SparkVersion serverVersion = newBuildAvailable();
        if (serverVersion == null) {
            UPDATING = false;
            UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
            if (explicit) {
                UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.no.updates"), Res.getString("title.no.updates"), JOptionPane.INFORMATION_MESSAGE);
            }
            return;
        }
        // Otherwise updates are available
        String downloadURL = serverVersion.getDownloadURL();
        String filename = downloadURL.substring(downloadURL.lastIndexOf("/") + 1);
        if (filename.indexOf('=') != -1) {
            filename = filename.substring(filename.indexOf('=') + 1);
        }
        // Set Download Directory
        final File downloadDir = new File(Spark.getSparkUserHome(), "updates");
        downloadDir.mkdirs();
        // Set file to download.
        final File fileToDownload = new File(downloadDir, filename);
        if (fileToDownload.exists()) {
            fileToDownload.delete();
        }
        ConfirmDialog confirm = new ConfirmDialog();
        confirm.showConfirmDialog(SparkManager.getMainWindow(), Res.getString("title.new.version.available"), Res.getString("message.new.spark.available", filename), Res.getString("yes"), Res.getString("no"), null);
        confirm.setDialogSize(400, 300);
        confirm.setConfirmListener(new ConfirmListener() {

            public void yesOption() {
                SwingWorker worker = new SwingWorker() {

                    public Object construct() {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            Log.error(e);
                        }
                        return "ok";
                    }

                    public void finished() {
                        if (Spark.isWindows()) {
                            downloadUpdate(fileToDownload, serverVersion);
                        } else {
                            // Launch browser to download page.
                            try {
                                if (sparkPluginInstalled) {
                                    BrowserLauncher.openURL(serverVersion.getDownloadURL());
                                } else {
                                    BrowserLauncher.openURL("http://www.igniterealtime.org/downloads/index.jsp#spark");
                                }
                            } catch (Exception e) {
                                Log.error(e);
                            }
                            UPDATING = false;
                        }
                    }
                };
                worker.start();
            }

            public void noOption() {
                UPDATING = false;
            }
        });
    } else {
        UPDATING = false;
    }
}
Also used : Calendar(java.util.Calendar) Date(java.util.Date) IOException(java.io.IOException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) ConfirmListener(org.jivesoftware.spark.component.ConfirmDialog.ConfirmListener) File(java.io.File) ConfirmDialog(org.jivesoftware.spark.component.ConfirmDialog)

Aggregations

IOException (java.io.IOException)2 ConfirmDialog (org.jivesoftware.spark.component.ConfirmDialog)2 ConfirmListener (org.jivesoftware.spark.component.ConfirmDialog.ConfirmListener)2 File (java.io.File)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 SwingWorker (org.jivesoftware.spark.util.SwingWorker)1 LocalPreferences (org.jivesoftware.sparkimpl.settings.local.LocalPreferences)1