Skip to content

Commit 8be044b

Browse files
committed
Add notification for plugin and theme #22
1 parent fe7741c commit 8be044b

8 files changed

Lines changed: 616 additions & 14 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
/**
45+
* depend on wp-cli.
46+
*
47+
* @author junichi11
48+
*/
49+
public class UpdateItem {
50+
51+
private final String status;
52+
private final String name;
53+
private final String version;
54+
55+
public UpdateItem(String status, String name, String version) {
56+
this.status = status;
57+
this.name = name;
58+
this.version = version;
59+
}
60+
61+
public String getStatus() {
62+
return status;
63+
}
64+
65+
public String getName() {
66+
return name;
67+
}
68+
69+
public String getVersion() {
70+
return version;
71+
}
72+
73+
public boolean isUpdate() {
74+
if (status == null) {
75+
return false;
76+
}
77+
return status.startsWith("U"); // NOI18N
78+
}
79+
80+
public static class Factory {
81+
82+
public Factory() {
83+
}
84+
85+
public static UpdateItem create(String data) {
86+
data = data.trim();
87+
data = data.replaceAll("\\s +", " "); // NOI18N
88+
String[] splitData = data.split(" "); // NOI18N
89+
if (splitData.length != 3) {
90+
return null;
91+
}
92+
return new UpdateItem(splitData[0], splitData[1], splitData[2]);
93+
}
94+
}
95+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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.util.ArrayList;
47+
import java.util.Arrays;
48+
import java.util.List;
49+
import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
50+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
51+
import org.netbeans.modules.php.api.util.StringUtils;
52+
import org.netbeans.modules.php.wordpress.commands.WordPressCli;
53+
import org.netbeans.modules.php.wordpress.ui.options.WordPressOptions;
54+
import org.openide.DialogDisplayer;
55+
import org.openide.NotifyDescriptor;
56+
import org.openide.awt.NotificationDisplayer;
57+
import org.openide.util.Exceptions;
58+
import org.openide.util.ImageUtilities;
59+
import org.openide.util.NbBundle;
60+
import org.openide.util.lookup.ServiceProvider;
61+
62+
@ServiceProvider(service = WordPressUpgradeChecker.class)
63+
public class WordPressPluginUpgradeChecker implements WordPressUpgradeChecker {
64+
65+
private boolean hasUpgrade = false;
66+
private ArrayList<UpdateItem> items;
67+
68+
@Override
69+
public boolean hasUpgrade(PhpModule phpModule) {
70+
WordPressOptions options = WordPressOptions.getInstance();
71+
if (StringUtils.isEmpty(options.getWpCliPath()) || !options.isCheckPluginNewVersion()) {
72+
return false;
73+
}
74+
for (UpdateItem item : getUpdateItems(phpModule)) {
75+
if (item.isUpdate()) {
76+
hasUpgrade = true;
77+
return hasUpgrade;
78+
}
79+
}
80+
hasUpgrade = false;
81+
return hasUpgrade;
82+
}
83+
84+
@NbBundle.Messages({
85+
"WordPressPluginUpgradeChecker.notify.title=Notification: New plugin version is available",
86+
"# {0} - status",
87+
"# {1} - project",
88+
"WordPressPluginUpgradeChecker.notify.detail={0} ({1})",})
89+
@Override
90+
public void notifyUpgrade(PhpModule phpModule) {
91+
if (!hasUpgrade) {
92+
return;
93+
}
94+
StringBuilder sb = new StringBuilder();
95+
for (UpdateItem item : items) {
96+
if (!item.isUpdate()) {
97+
continue;
98+
}
99+
sb.append(String.format(" %s:%s", item.getName(), item.getVersion())); // NOI18N
100+
}
101+
NotificationDisplayer.getDefault().notify(
102+
Bundle.WordPressPluginUpgradeChecker_notify_title(),
103+
ImageUtilities.loadImageIcon(WordPress.WP_ICON_16, false),
104+
Bundle.WordPressPluginUpgradeChecker_notify_detail(sb.toString(), phpModule.getDisplayName()),
105+
new PluginUpdateActionListener(phpModule)
106+
);
107+
}
108+
109+
private List<UpdateItem> getUpdateItems(PhpModule phpModule) {
110+
items = new ArrayList<UpdateItem>();
111+
try {
112+
// get result
113+
WordPressCli wpCli = WordPressCli.getDefault(false);
114+
List<String> lines = wpCli.getPluginStatus(phpModule);
115+
boolean isStart = false;
116+
for (String line : lines) {
117+
if (line.isEmpty()) {
118+
break;
119+
}
120+
if (line.endsWith(":")) { // NOI18N
121+
isStart = true;
122+
continue;
123+
}
124+
if (!isStart) {
125+
continue;
126+
}
127+
UpdateItem item = UpdateItem.Factory.create(line);
128+
if (item != null) {
129+
items.add(item);
130+
}
131+
}
132+
} catch (InvalidPhpExecutableException ex) {
133+
Exceptions.printStackTrace(ex);
134+
}
135+
return items;
136+
}
137+
138+
//~ Inner classes
139+
private static class PluginUpdateActionListener implements ActionListener {
140+
141+
private final PhpModule phpModule;
142+
143+
public PluginUpdateActionListener(PhpModule phpModule) {
144+
this.phpModule = phpModule;
145+
}
146+
147+
@NbBundle.Messages("PluginUpdateActionListener.comfirmation=Do you want to update? (run wp plugin update --all)")
148+
@Override
149+
public void actionPerformed(ActionEvent e) {
150+
if (StringUtils.isEmpty(WordPressOptions.getInstance().getWpCliPath())) {
151+
return;
152+
}
153+
154+
// confirmation
155+
NotifyDescriptor.Confirmation comfirmation = new NotifyDescriptor.Confirmation(
156+
Bundle.PluginUpdateActionListener_comfirmation(),
157+
NotifyDescriptor.OK_CANCEL_OPTION
158+
);
159+
if (DialogDisplayer.getDefault().notify(comfirmation) != NotifyDescriptor.OK_OPTION) {
160+
return;
161+
}
162+
try {
163+
// plugin update --all
164+
WordPressCli wpCli = WordPressCli.getDefault(true);
165+
wpCli.pluginUpdate(phpModule, Arrays.asList(WordPressCli.ALL_PARAM));
166+
} catch (InvalidPhpExecutableException ex) {
167+
Exceptions.printStackTrace(ex);
168+
}
169+
170+
}
171+
}
172+
173+
}

0 commit comments

Comments
 (0)