package net.sourceforge.phpeclipse.wiki.actions.blogwiki; import java.util.ArrayList; import java.util.Collections; import java.util.List; import net.sourceforge.phpeclipse.wiki.blog.MetaWeblog; import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction; import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.internal.Configuration; import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager; import net.sourceforge.phpeclipse.wiki.internal.ConfigurationWorkingCopy; import net.sourceforge.phpeclipse.wiki.internal.IConfiguration; import net.sourceforge.phpeclipse.wiki.preferences.Util; import org.eclipse.core.resources.IFile; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; import org.eclipse.ui.IEditorActionDelegate; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.dialogs.ListSelectionDialog; import org.eclipse.ui.internal.dialogs.ListContentProvider; import org.eclipse.ui.texteditor.AbstractTextEditor; public final class NewPostBlogWikiAction implements IEditorActionDelegate { // public static String APPKEY = // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83"; private IWorkbenchWindow window; private AbstractTextEditor fEditor; public void dispose() { } public void init(IWorkbenchWindow window) { this.window = window; } public void selectionChanged(IAction action, ISelection selection) { if (selection.isEmpty()) { return; } if (selection instanceof TextSelection) { action.setEnabled(true); return; } if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) { action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class)); } } protected ConfigurationWorkingCopy getConfiguration() { List allConfigsList = ConfigurationManager.getInstance().getConfigurations(); ArrayList configsList = new ArrayList(); for (int i = 0; i < allConfigsList.size(); i++) { IConfiguration temp = (IConfiguration) allConfigsList.get(i); if (temp.getType().equals(WikiEditorPlugin.BLOG_A_WIKI)) { configsList.add(temp); } } Collections.sort(configsList); ConfigurationWorkingCopy configuration = null; ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench() .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(), "Select blog configuration for your wiki text."); listSelectionDialog.setTitle("Multiple active configuration found"); if (listSelectionDialog.open() == Window.OK) { Object[] locations = listSelectionDialog.getResult(); if (locations != null) { for (int i = 0; i < locations.length; i++) { configuration = (ConfigurationWorkingCopy) locations[i]; break; } } } return configuration; } public void run(IAction action) { if (fEditor == null) { IEditorPart targetEditor = window.getActivePage().getActiveEditor(); if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; } } if (fEditor != null) { boolean cache = true; ConfigurationWorkingCopy config = getConfiguration(); //new Configuration("http://localhost:8080/snip/RPC2", "1", "admin", // "admin"); try { IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput(); IFile file = ei.getFile(); StringBuffer htmlBuffer = new StringBuffer(); CreatePageAction.getWikiBuffer(htmlBuffer, file); ArrayList images = new ArrayList(); getImages(htmlBuffer, images); String[] result = new String[2]; cache = config.promptForPassword(config.getUser(), "Insert Config", true, result); if (result[0] == null || result[1] == null) { return; } if (result[0].equals("") || result[1].equals("")) { return; } String title = Util.getWikiTitle(file); if (title != null) { config.setName(result[0]); config.setPassword(result[1]); MetaWeblog metaWebLog = new MetaWeblog(config); String guid = metaWebLog.newPost(file, title, htmlBuffer, true); System.out.println(guid); if (images.size() > 0) { String fullImagePath; String filePath = file.getLocation().toString(); int index = filePath.lastIndexOf('/'); if (index >= 0) { filePath = filePath.substring(0, index + 1); } for (int i = 0; i < images.size(); i++) { fullImagePath = filePath + "Image/" + images.get(i).toString(); metaWebLog.publishAttachement(guid, fullImagePath, false); } } } else { MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp"); } } catch (Exception e) { MessageDialog.openError(null, "Exception: ", e.toString()); e.printStackTrace(); } finally { if (!cache && config != null) { // reset password config.setPassword(""); } } } } /** * @param content * the wikitext * @param images * result List of image names */ private void getImages(StringBuffer content, List images) { int startIndex = 0; int endIndex = 0; String imageName; while (startIndex >= 0) { startIndex = content.indexOf("[[Image:", startIndex); if (startIndex >= 0) { endIndex = content.indexOf("]]", startIndex + 8); if (endIndex < 0) { return; } imageName = content.substring(startIndex + 8, endIndex); images.add(imageName); startIndex += 8; } } } public void setActiveEditor(IAction action, IEditorPart targetEditor) { if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; } } }