misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / NewPostBlogEditorAction.java
1 package net.sourceforge.phpeclipse.wiki.actions;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import net.sourceforge.phpeclipse.wiki.blog.Configuration;
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.preferences.Util;
11
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.text.TextSelection;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.ui.IEditorActionDelegate;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IFileEditorInput;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.texteditor.AbstractTextEditor;
22
23 public final class NewPostBlogEditorAction implements IEditorActionDelegate {
24   //  public static String APPKEY =
25   // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
26
27   private IWorkbenchWindow window;
28
29   private AbstractTextEditor fEditor;
30
31   public void dispose() {
32   }
33
34   public void init(IWorkbenchWindow window) {
35     this.window = window;
36   }
37
38   public void selectionChanged(IAction action, ISelection selection) {
39     if (selection.isEmpty()) {
40       return;
41     }
42     if (selection instanceof TextSelection) {
43       action.setEnabled(true);
44       return;
45     }
46     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
47       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
48     }
49   }
50
51   public void run(IAction action) {
52     if (fEditor == null) {
53       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
54       if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
55         fEditor = (AbstractTextEditor) targetEditor;
56       }
57     }
58     if (fEditor != null) {
59       try {
60         Configuration config = new Configuration("http://localhost:8080/snip/RPC2", "1", "admin", "admin");
61         IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
62         IFile file = ei.getFile();
63         StringBuffer htmlBuffer = new StringBuffer();
64         CreatePageAction.getWikiBuffer(htmlBuffer,file);
65
66         ArrayList images = new ArrayList();
67         getImages(htmlBuffer, images);
68
69         String[] result = new String[2];
70         boolean cache = config.promptForPassword(config.getUser(), "Insert Config", true, result);
71         if (result[0] == null || result[1] == null) {
72           return;
73         }
74         if (result[0].equals("") || result[1].equals("")) {
75           return;
76         }
77
78         String title = Util.getWikiTitle(file);
79         if (title != null) {
80           MetaWeblog metaWebLog = new MetaWeblog(config);
81           String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
82           System.out.println(guid);
83
84           if (images.size() > 0) {
85             String fullImagePath;
86             String filePath = file.getLocation().toString();
87             int index = filePath.lastIndexOf('/');
88             if (index>=0) {
89               filePath = filePath.substring(0,index+1);
90             }
91             for (int i = 0; i < images.size(); i++) {
92               fullImagePath = filePath+"Image/"+images.get(i).toString();
93               metaWebLog.publishAttachement(guid, fullImagePath, false);
94             }
95           }
96         } else {
97           MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
98         }
99       } catch (Exception e) {
100         MessageDialog.openError(null, "Exception: ", e.toString());
101         e.printStackTrace();
102       }
103     }
104   }
105
106   /**
107    * @param content the wikitext
108    * @param images result List of image names
109    */
110   private void getImages(StringBuffer content, List images) {
111     int startIndex = 0;
112     int endIndex = 0;
113     String imageName;
114     while (startIndex >= 0) {
115       startIndex = content.indexOf("[[Image:", startIndex);
116       if (startIndex >= 0) {
117         endIndex = content.indexOf("]]", startIndex + 8);
118         if (endIndex < 0) {
119           return;
120         }
121         imageName = content.substring(startIndex + 8, endIndex);
122         images.add(imageName);
123         startIndex += 8;
124       }
125     }
126   }
127
128   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
129     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
130       fEditor = (AbstractTextEditor) targetEditor;
131     }
132   }
133
134 }