~do-plugins/do-plugins/trunk

« back to all changes in this revision

Viewing changes to Dropbox/src/DropboxPuburlAction.cs

  • Committer: Stephen Elson
  • Date: 2009-05-07 21:32:56 UTC
  • mto: This revision was merged to the branch mainline in revision 619.
  • Revision ID: stephen.elson@gmail.com-20090507213256-wdlhj0ou2l59sith
Initial commit of Dropbox plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.Linq;
 
4
using System.Diagnostics;
 
5
using System.Collections.Generic;
 
6
 
 
7
using Do.Universe;
 
8
using Do.Universe.Common;
 
9
using Do.Platform;
 
10
 
 
11
using Mono.Unix;
 
12
 
 
13
 
 
14
namespace Dropbox
 
15
{
 
16
        
 
17
        
 
18
        public class DropboxPuburlAction : Act
 
19
        {
 
20
                
 
21
                private string dropbox_dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal) + "/Dropbox";
 
22
                
 
23
                public override string Name {
 
24
                        get { return "Public URL";  }
 
25
                }
 
26
                
 
27
                public override string Description {
 
28
                        get { return "Get public url of a file in your dropbox."; }
 
29
                }
 
30
                
 
31
                public override string Icon {
 
32
                        get { return "dropbox"; }
 
33
                }
 
34
                
 
35
                public override IEnumerable<Type> SupportedItemTypes {
 
36
                        get { yield return typeof (IFileItem); }
 
37
                }
 
38
                
 
39
                public override bool SupportsItem (Item item) 
 
40
                {
 
41
                        return (item as IFileItem).Path.StartsWith (dropbox_dir);
 
42
                }
 
43
                
 
44
                public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modItems)
 
45
                {
 
46
                        string path = (items.First () as IFileItem).Path;
 
47
                        string url = GetPubUrl (path);
 
48
                        
 
49
                        if (url == "") {
 
50
                                
 
51
                                string msg = String.Format ("Sorry, the file \"{0}\" is not public", 
 
52
                                        UnixPath.GetFileName (path));
 
53
                                
 
54
                                Log<DropboxPuburlAction>.Debug (msg);
 
55
                                Notification notification = new Notification ("Dropbox", msg, "dropbox");
 
56
                                Services.Notifications.Notify (notification);
 
57
                                
 
58
                        } else {
 
59
                                yield return new BookmarkItem (url, url);
 
60
                        }
 
61
                        
 
62
                }
 
63
                
 
64
                private string GetPubUrl (string path)
 
65
                {
 
66
                        string url = "";
 
67
                        
 
68
                        try {
 
69
                                ProcessStartInfo cmd = new ProcessStartInfo ();
 
70
                                cmd.FileName = "dropbox";
 
71
                                cmd.Arguments = String.Format ("puburl \"{0}\"", path); 
 
72
                                cmd.UseShellExecute = false;
 
73
                                cmd.RedirectStandardOutput = true;
 
74
                                
 
75
                                Process run = Process.Start (cmd);
 
76
                                run.WaitForExit ();
 
77
                                
 
78
                                url = run.StandardOutput.ReadLine ();
 
79
                                if (!url.StartsWith ("http")) { url = ""; }
 
80
                                
 
81
                        } catch {
 
82
                        }
 
83
                        
 
84
                        return url;
 
85
                }
 
86
 
 
87
        }
 
88
}