とりあえずWindows Live Photo Galleryのプラグインを作るぞー!ということでサクッと作ってみたBlogThisPlugin。書くことが無さ過ぎて、前のエントリは無駄に英語で書いてみたりしてみましたが、このエントリでは、このBlogThisPluginを作るにあたってWindows Live Photo Gallery Publishing Plug-in Platformを調べて分かったことを記したいと思います。ちなみに、Windows Live Photo Gallery Publishing Plug-in PlatformとはWindows Live Photo Galleryの拡張をホストする機能のことで、これを使えば「投稿」メニューに独自の項目を追加することが出来ます。
Windows Live Photo GalleryのPublishing Plug-in(以下Publishing Plug-in)の作成はIPublishPlugin Interfaceを実装したクラスを作成し、そのアセンブリのパスやIPublishPlugin Interfaceを実装したクラスの完全限定名などをPublishing Plug-in Registry Settingsに従ってレジストリに登録すれば出来上がり、という流れです。属性を活用していたWLWに比べると、ちょっとレガシーですね。IPublishPlugin Interfaceを実装するにあたってはPublishing Plug-in Architectureを参照し、どのメソッドがどのタイミングで呼び出されるかを確認するとよいでしょう。メソッドの呼び出しの合間に、Live Photo Galleryが進捗状況を表示するダイアログを挟みこんできたりするので…。また、Publishing Plug-in Platformは、IPublishPluginのメソッドを呼び出す際、IPublishPlugin Interfaceを実装したクラスのインスタンスを使いまわさず、インスタンスを生成し直してくる場合があるので、その点にも注意した方がよいでしょう。
折角なので、BlogThisPluginのソースコードを晒してみます。
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.WindowsLive.PublishPlugins;
using WindowsLive.Writer.Api;
namespace SharpLab.BlogThisPlugin {
public class PublishPlugin : IPublishPlugin{
public bool HasPublishResults(System.Xml.XmlDocument sessionXml) {
throw new NotImplementedException();
}
public bool HasSummaryInformation(System.Xml.XmlDocument sessionXml) {
throw new NotImplementedException();
}
public void LaunchPublishResults(System.Xml.XmlDocument sessionXml) {
throw new NotImplementedException();
}
public bool PublishItem(System.Windows.Forms.IWin32Window parentWindow, string mediaObjectId, System.IO.Stream stream, System.Xml.XmlDocument sessionXml, IPublishProperties publishProperties, IPublishProgressCallback callback, System.Threading.EventWaitHandle cancelEvent) {
throw new NotImplementedException();
}
public void ShowSummaryInformation(System.Windows.Forms.IWin32Window parentWindow, System.Xml.XmlDocument sessionXml) {
throw new NotImplementedException();
}
public bool ShowConfigurationSettings(System.Windows.Forms.IWin32Window parentWindow, System.Xml.XmlDocument sessionXml, System.Xml.XmlDocument persistXml, IPublishProperties publishProperties) {
if(!WriterApplication.IsInstalled){
System.Windows.Forms.MessageBox.Show(parentWindow, "Windows Live Writer is not Installed.", "BlogThis Plugin",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else{
blogThis(parentWindow, sessionXml);
}
return false;
}
private void blogThis(System.Windows.Forms.IWin32Window parentWindow, System.Xml.XmlDocument sessionXml) {
XmlNodeList items = sessionXml.SelectNodes("/PhotoGalleryPublishSession/ItemSet/Item[PerceivedType="image"]/FullFilePath");
if(items.Count==0){
System.Windows.Forms.MessageBox.Show(parentWindow, "Please select at least one image.", "BlogThis Plugin",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
return;
}
string[] fileNames = new string[items.Count];
for (int i = 0; i < items.Count; i++) {
fileNames[i] = items[i].InnerText;
}
WriterApplication writer = new WriterApplication();
writer.BlogThisImageFileList(null, fileNames, null);
}
}
}
うわぁNotImplementedExceptionばっか。。。BlogThisPluginでは、最初に呼び出されるShowConfigurationSettingsメソッドの段階でfalseを返してしまうことにしました。これは、写真のアップロードを直接するわけではないため、Live Photo Galleryの挟みこんでくるダイアログが邪魔だったからです。まぁ、こういうのもありかもね、ということで。そのうちにPublishing Plug-inのアーキテクチャに沿ってWordPressにXMLRPC-APIを使って画像やらを投稿するプラグインも試作してみたいと思います。
ちなみにソリューション全体はこちらにアップしています。
http://depot.sharplab.net/CSharp/WLWPlugin/BlogThisPluginSln.zip
真っ当に実装されたMS謹製のFlickerのプラグインより簡単に弄れるたたき台としてどうぞ。
BlogThisPlugin