|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| 7 | + * Other names may be trademarks of their respective owners. |
| 8 | + * |
| 9 | + * The contents of this file are subject to the terms of either the GNU |
| 10 | + * General Public License Version 2 only ("GPL") or the Common |
| 11 | + * Development and Distribution License("CDDL") (collectively, the |
| 12 | + * "License"). You may not use this file except in compliance with the |
| 13 | + * License. You can obtain a copy of the License at |
| 14 | + * http://www.netbeans.org/cddl-gplv2.html |
| 15 | + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
| 16 | + * specific language governing permissions and limitations under the |
| 17 | + * License. When distributing the software, include this License Header |
| 18 | + * Notice in each file and include the License file at |
| 19 | + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
| 20 | + * particular file as subject to the "Classpath" exception as provided |
| 21 | + * by Oracle in the GPL Version 2 section of the License file that |
| 22 | + * accompanied this code. If applicable, add the following below the |
| 23 | + * License Header, with the fields enclosed by brackets [] replaced by |
| 24 | + * your own identifying information: |
| 25 | + * "Portions Copyrighted [year] [name of copyright owner]" |
| 26 | + * |
| 27 | + * If you wish your version of this file to be governed by only the CDDL |
| 28 | + * or only the GPL Version 2, indicate your decision by adding |
| 29 | + * "[Contributor] elects to include this software in this distribution |
| 30 | + * under the [CDDL or GPL Version 2] license." If you do not indicate a |
| 31 | + * single choice of license, a recipient has the option to distribute |
| 32 | + * your version of this file under either the CDDL, the GPL Version 2 or |
| 33 | + * to extend the choice of license to its licensees as provided above. |
| 34 | + * However, if you add GPL Version 2 code and therefore, elected the GPL |
| 35 | + * Version 2 license, then the option applies only if the new code is |
| 36 | + * made subject to such option by the copyright holder. |
| 37 | + * |
| 38 | + * Contributor(s): |
| 39 | + * |
| 40 | + * Portions Copyrighted 2013 Sun Microsystems, Inc. |
| 41 | + */ |
| 42 | +package org.netbeans.modules.php.wordpress; |
| 43 | + |
| 44 | +import java.awt.event.ActionEvent; |
| 45 | +import java.awt.event.ActionListener; |
| 46 | +import java.io.IOException; |
| 47 | +import java.net.MalformedURLException; |
| 48 | +import java.util.Collections; |
| 49 | +import java.util.logging.Level; |
| 50 | +import java.util.logging.Logger; |
| 51 | +import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException; |
| 52 | +import org.netbeans.modules.php.api.phpmodule.PhpModule; |
| 53 | +import org.netbeans.modules.php.api.util.StringUtils; |
| 54 | +import org.netbeans.modules.php.wordpress.wpapis.WordPressVersionCheckApi; |
| 55 | +import org.netbeans.modules.php.wordpress.commands.WordPressCli; |
| 56 | +import org.netbeans.modules.php.wordpress.ui.options.WordPressOptions; |
| 57 | +import org.openide.DialogDisplayer; |
| 58 | +import org.openide.NotifyDescriptor; |
| 59 | +import org.openide.awt.NotificationDisplayer; |
| 60 | +import org.openide.util.Exceptions; |
| 61 | +import org.openide.util.ImageUtilities; |
| 62 | +import org.openide.util.NbBundle; |
| 63 | +import org.openide.util.lookup.ServiceProvider; |
| 64 | + |
| 65 | +/** |
| 66 | + * |
| 67 | + * @author junichi11 |
| 68 | + */ |
| 69 | +@ServiceProvider(service = WordPressUpgradeChecker.class) |
| 70 | +public final class WordPressCoreUpgradeChecker implements WordPressUpgradeChecker { |
| 71 | + |
| 72 | + private String upgradeVersionNumber; |
| 73 | + private static final Logger LOGGER = Logger.getLogger(WordPressCoreUpgradeChecker.class.getName()); |
| 74 | + |
| 75 | + public WordPressCoreUpgradeChecker() { |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean hasUpgrade(PhpModule phpModule) { |
| 80 | + try { |
| 81 | + if (StringUtils.isEmpty(upgradeVersionNumber)) { |
| 82 | + WordPressVersionCheckApi versionCheckApi = new WordPressVersionCheckApi(); |
| 83 | + versionCheckApi.parse(); |
| 84 | + upgradeVersionNumber = versionCheckApi.getVersion(); |
| 85 | + if (StringUtils.isEmpty(upgradeVersionNumber)) { |
| 86 | + // XXX throw exception? |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + WordPressVersion version = WordPressVersion.create(phpModule); |
| 92 | + return hasUpgrade(version); |
| 93 | + } catch (MalformedURLException ex) { |
| 94 | + LOGGER.log(Level.WARNING, ex.getLocalizedMessage()); |
| 95 | + } catch (IOException ex) { |
| 96 | + LOGGER.log(Level.WARNING, "Isn't connected to Internet:{0}", ex.getLocalizedMessage()); |
| 97 | + } |
| 98 | + |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Compare with current version number. |
| 104 | + * |
| 105 | + * @param version current version |
| 106 | + * @return true new version available, false otherwise |
| 107 | + * @throws NumberFormatException |
| 108 | + */ |
| 109 | + private boolean hasUpgrade(WordPressVersion version) throws NumberFormatException { |
| 110 | + // compare version |
| 111 | + String[] splitVersion = upgradeVersionNumber.split("[.]"); // NOI18N |
| 112 | + int length = splitVersion.length; |
| 113 | + if (length >= 2) { |
| 114 | + int majorVersion = Integer.parseInt(splitVersion[0]); |
| 115 | + int minorVersion = Integer.parseInt(splitVersion[1]); |
| 116 | + if (version.getMajor() < majorVersion) { |
| 117 | + return true; |
| 118 | + } else if (version.isMajor(majorVersion)) { |
| 119 | + if (version.getMinor() < minorVersion) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + if (length >= 3) { |
| 125 | + int revisionVersion = Integer.parseInt(splitVersion[2]); |
| 126 | + if (version.getRevision() < revisionVersion) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + @NbBundle.Messages({ |
| 134 | + "WordPressUpgradeChecker.core.notify.title=Notification: New version is available", |
| 135 | + "# {0} - version number", |
| 136 | + "# {1} - display name", |
| 137 | + "WordPressUpgradeChecker.core.notify.detail=New version is available: {0} ({1})",}) |
| 138 | + @Override |
| 139 | + public void notifyUpgrade(PhpModule phpModule) { |
| 140 | + if (StringUtils.isEmpty(upgradeVersionNumber)) { |
| 141 | + return; |
| 142 | + } |
| 143 | + NotificationDisplayer.getDefault().notify( |
| 144 | + Bundle.WordPressUpgradeChecker_core_notify_title(), |
| 145 | + ImageUtilities.loadImageIcon(WordPress.WP_ICON_16, false), |
| 146 | + Bundle.WordPressUpgradeChecker_core_notify_detail(upgradeVersionNumber, phpModule.getDisplayName()), |
| 147 | + new CoreUpdateActionListener(phpModule) |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + public String getUpgradeVersionNumber() { |
| 152 | + return upgradeVersionNumber; |
| 153 | + } |
| 154 | + |
| 155 | + //~ Inner classes |
| 156 | + private static class CoreUpdateActionListener implements ActionListener { |
| 157 | + |
| 158 | + private final PhpModule phpModule; |
| 159 | + |
| 160 | + public CoreUpdateActionListener(PhpModule phpModule) { |
| 161 | + this.phpModule = phpModule; |
| 162 | + } |
| 163 | + |
| 164 | + @NbBundle.Messages("CoreUpdateActionListener.comfirmation=Do you want to update? (run wp core update)") |
| 165 | + @Override |
| 166 | + public void actionPerformed(ActionEvent e) { |
| 167 | + if (StringUtils.isEmpty(WordPressOptions.getInstance().getWpCliPath())) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + // confirmation |
| 172 | + NotifyDescriptor.Confirmation comfirmation = new NotifyDescriptor.Confirmation( |
| 173 | + Bundle.CoreUpdateActionListener_comfirmation(), |
| 174 | + NotifyDescriptor.OK_CANCEL_OPTION |
| 175 | + ); |
| 176 | + if (DialogDisplayer.getDefault().notify(comfirmation) != NotifyDescriptor.OK_OPTION) { |
| 177 | + return; |
| 178 | + } |
| 179 | + try { |
| 180 | + WordPressCli wpCli = WordPressCli.getDefault(true); |
| 181 | + wpCli.coreUpdate(phpModule, Collections.<String>emptyList()); |
| 182 | + } catch (InvalidPhpExecutableException ex) { |
| 183 | + Exceptions.printStackTrace(ex); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments