/HatenaPhotLife/HatenaPhotLifeUploader.cs
C# | 111 lines | 96 code | 15 blank | 0 comment | 7 complexity | f420684553981fa011366ca982471c8f MD5 | raw file
- using System;
- using System.IO;
- using System.Net;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using InstantImageUploader.Models;
- using HatenaPhotLife.Properties;
- using System.Reactive.Linq;
- using System.Diagnostics;
- using System.ComponentModel.Composition;
-
- namespace HatenaPhotLife
- {
- [Export(typeof(IImageUploader))]
- public class HatenaPhotLifeUploader : IImageUploader
- {
- public string ImageUploaderName
- {
- get { return "はてなフォトライフ"; }
- }
-
- public string Description
- {
- get { return "はてなフォトライフに画像をアップロードします。"; }
- }
-
- public string Author
- {
- get { return "Masanori Onoue (ugaya40)"; }
- }
-
- public string Url
- {
- get { return "http://ugaya40.net"; }
- }
-
- public string UserName
- {
- get;
- private set;
- }
-
- public string Password
- {
- get;
- private set;
- }
-
- public string FolderName
- {
- get;
- private set;
- }
-
- public void Initialize()
- {
- Settings.Default.Reload();
-
- if ((!string.IsNullOrEmpty(Settings.Default.UserName))
- && (!string.IsNullOrEmpty(Settings.Default.Password)))
- {
- UserName = Settings.Default.UserName;
- Password = Settings.Default.Password;
- FolderName = Settings.Default.FolderName;
- }
- }
-
- public void Upload(string imageFilePath)
- {
- Observable.Defer(() => new HatenaPhotLifeClient(UserName, Password).UploadAsObservable(imageFilePath,FolderName))
- .Retry(3)
- .Subscribe(url =>
- {
- Process.Start(url);
- OnSuccessImageUpload(null);
- },(Exception ex) => OnFailedImageUpload("はてなフォトライフへのアップロードに失敗しました"));
- }
-
- public bool IsNeedSetting
- {
- get { return string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password); }
- }
-
- public event EventHandler<SuccessImageUploadEventArgs> SuccessImageUpload;
- private void OnSuccessImageUpload(string messageForUser)
- {
- var handler = SuccessImageUpload;
- if (handler != null)
- {
- handler(this, new SuccessImageUploadEventArgs(messageForUser));
- }
- }
-
- public event EventHandler<FailedImageUploadEventArgs> FailedImageUpload;
- private void OnFailedImageUpload(string messageForUser)
- {
- var handler = FailedImageUpload;
- if (handler != null)
- {
- handler(this, new FailedImageUploadEventArgs(messageForUser));
- }
- }
-
- public Type SettingWindowType
- {
- get { return typeof(SettingWindow); }
- }
- }
-
- }