Upload/Download of multiple files now possible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / blogwiki / NewPostBlogWikiAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.blogwiki;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import net.sourceforge.phpeclipse.wiki.blog.MetaWeblog;
8 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
9 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
10 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
11 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
12 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
13 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationWorkingCopy;
14 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
15 import net.sourceforge.phpeclipse.wiki.preferences.Util;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.text.TextSelection;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.LabelProvider;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.ui.IEditorActionDelegate;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IFileEditorInput;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.dialogs.ListSelectionDialog;
29 import org.eclipse.ui.internal.dialogs.ListContentProvider;
30 import org.eclipse.ui.texteditor.AbstractTextEditor;
31
32 public final class NewPostBlogWikiAction implements IEditorActionDelegate {
33   //  public static String APPKEY =
34   // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
35
36   private IWorkbenchWindow window;
37
38   private AbstractTextEditor fEditor;
39
40   public void dispose() {
41   }
42
43   public void init(IWorkbenchWindow window) {
44     this.window = window;
45   }
46
47   public void selectionChanged(IAction action, ISelection selection) {
48     if (selection.isEmpty()) {
49       return;
50     }
51     if (selection instanceof TextSelection) {
52       action.setEnabled(true);
53       return;
54     }
55     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
56       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
57     }
58   }
59
60   protected ConfigurationWorkingCopy getConfiguration() {
61     List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
62     ArrayList configsList = new ArrayList();
63     for (int i = 0; i < allConfigsList.size(); i++) {
64       IConfiguration temp = (IConfiguration) allConfigsList.get(i);
65       if (temp.getType().equals(WikiEditorPlugin.BLOG_A_WIKI)) {
66         configsList.add(temp);
67       }
68     }
69     Collections.sort(configsList);
70     ConfigurationWorkingCopy configuration = null;
71     ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
72         .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
73         "Select blog configuration for your wiki text.");
74     listSelectionDialog.setTitle("Multiple active configuration found");
75     if (listSelectionDialog.open() == Window.OK) {
76       Object[] locations = listSelectionDialog.getResult();
77       if (locations != null) {
78         for (int i = 0; i < locations.length; i++) {
79           configuration = (ConfigurationWorkingCopy) locations[i];
80           break;
81         }
82       }
83     }
84     return configuration;
85   }
86
87   public void run(IAction action) {
88     if (fEditor == null) {
89       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
90       if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
91         fEditor = (AbstractTextEditor) targetEditor;
92       }
93     }
94     if (fEditor != null) {
95       boolean cache = true;
96       ConfigurationWorkingCopy config = getConfiguration(); //new Configuration("http://localhost:8080/snip/RPC2", "1", "admin",
97       // "admin");
98       try {
99         IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
100         IFile file = ei.getFile();
101         StringBuffer htmlBuffer = new StringBuffer();
102         CreatePageAction.getWikiBuffer(htmlBuffer, file);
103
104         ArrayList images = new ArrayList();
105         getImages(htmlBuffer, images);
106
107         String[] result = new String[2];
108         cache = config.promptForPassword(config.getUser(), "Insert Config", true, result);
109         if (result[0] == null || result[1] == null) {
110           return;
111         }
112         if (result[0].equals("") || result[1].equals("")) {
113           return;
114         }
115
116         String title = Util.getWikiTitle(file);
117         if (title != null) {
118           config.setName(result[0]);
119           config.setPassword(result[1]);
120           MetaWeblog metaWebLog = new MetaWeblog(config);
121           String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
122           System.out.println(guid);
123
124           if (images.size() > 0) {
125             String fullImagePath;
126             String filePath = file.getLocation().toString();
127             int index = filePath.lastIndexOf('/');
128             if (index >= 0) {
129               filePath = filePath.substring(0, index + 1);
130             }
131             for (int i = 0; i < images.size(); i++) {
132               fullImagePath = filePath + "Image/" + images.get(i).toString();
133               metaWebLog.publishAttachement(guid, fullImagePath, false);
134             }
135           }
136         } else {
137           MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
138         }
139       } catch (Exception e) {
140         MessageDialog.openError(null, "Exception: ", e.toString());
141         e.printStackTrace();
142       } finally {
143         if (!cache && config != null) {
144           // reset password
145           config.setPassword("");
146         }
147       }
148     }
149   }
150
151   /**
152    * @param content
153    *          the wikitext
154    * @param images
155    *          result List of image names
156    */
157   private void getImages(StringBuffer content, List images) {
158     int startIndex = 0;
159     int endIndex = 0;
160     String imageName;
161     while (startIndex >= 0) {
162       startIndex = content.indexOf("[[Image:", startIndex);
163       if (startIndex >= 0) {
164         endIndex = content.indexOf("]]", startIndex + 8);
165         if (endIndex < 0) {
166           return;
167         }
168         imageName = content.substring(startIndex + 8, endIndex);
169         images.add(imageName);
170         startIndex += 8;
171       }
172     }
173   }
174
175   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
176     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
177       fEditor = (AbstractTextEditor) targetEditor;
178     }
179   }
180
181 }