~cszikszoy/do-plugins/fix-confluence

« back to all changes in this revision

Viewing changes to Dropbox/src/DropboxAbstractAction.cs

  • Committer: Chris S.
  • Date: 2009-06-10 17:32:56 UTC
  • mfrom: (599.3.32 Dropbox)
  • Revision ID: chris@szikszoy.com-20090610173256-ndhy3yztbqjc4zs0
merge Dropbox plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// DropboxAbstractAction.cs
 
3
// 
 
4
// GNOME Do is the legal property of its developers. Please refer to the
 
5
// COPYRIGHT file distributed with this
 
6
// source distribution.
 
7
// 
 
8
// This program is free software: you can redistribute it and/or modify
 
9
// it under the terms of the GNU General Public License as published by
 
10
// the Free Software Foundation, either version 3 of the License, or
 
11
// (at your option) any later version.
 
12
// 
 
13
// This program is distributed in the hope that it will be useful,
 
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
// GNU General Public License for more details.
 
17
// 
 
18
// You should have received a copy of the GNU General Public License
 
19
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
// 
 
21
 
 
22
using System;
 
23
using System.IO;
 
24
using System.Diagnostics;
 
25
using System.Collections.Generic;
 
26
 
 
27
using Mono.Unix;
 
28
 
 
29
using Do.Universe;
 
30
using Do.Platform;
 
31
 
 
32
namespace Dropbox
 
33
{
 
34
        
 
35
        
 
36
        public abstract class DropboxAbstractAction : Act
 
37
        {
 
38
                
 
39
                protected static Random rand = new Random ();
 
40
                protected string pub_url_title = Catalog.GetString ("URL of your shared file");
 
41
        
 
42
        
 
43
                public override IEnumerable<Type> SupportedItemTypes {
 
44
                        get { yield return typeof (IFileItem); }
 
45
                }
 
46
                
 
47
                protected void Notify (string message)
 
48
                {
 
49
                        Notification notification = new Notification (Catalog.GetString ("Dropbox"), message, "dropbox");
 
50
                        Services.Notifications.Notify (notification);
 
51
                }
 
52
                
 
53
                protected string ReadLink (string link_name)
 
54
                {
 
55
                        return RunProcess ("readlink", string.Format ("\"{0}\"", link_name));
 
56
                }
 
57
                
 
58
                protected bool MakeLink (string target, string link_name)
 
59
                {
 
60
                        string result = RunProcess ("ln", 
 
61
                                string.Format ("-s \"{0}\" \"{1}\"", target, link_name));
 
62
                        
 
63
                        return result != null;
 
64
                }
 
65
                
 
66
                protected bool Unlink (string link_name)
 
67
                {
 
68
                        string result = RunProcess ("unlink", 
 
69
                                string.Format ("\"{0}\"", link_name));
 
70
 
 
71
                        return result != null;
 
72
                }
 
73
                
 
74
                protected string GetLink (string target)
 
75
                {
 
76
                        return GetLink (target, Dropbox.DoSharedPath);
 
77
                }
 
78
                
 
79
                protected string GetLink (string target, string directory)
 
80
                {
 
81
                        if (!Directory.Exists (directory))
 
82
                                return null;
 
83
                        
 
84
                        foreach (string file in Directory.GetFiles (directory)) 
 
85
                                if (ReadLink (file) == target) 
 
86
                                        return file;
 
87
 
 
88
                        return null;
 
89
                }
 
90
                
 
91
                protected bool HasLink (string target)
 
92
                {
 
93
                        return GetLink (target) != null;
 
94
                }
 
95
                
 
96
                protected bool HasLink (string target, string directory)
 
97
                {
 
98
                        return GetLink (target, directory) != null;
 
99
                }
 
100
                
 
101
                protected string GetPath (Item item)
 
102
                {
 
103
                        if (item is IFileItem)
 
104
                                return GetPath (item as IFileItem);
 
105
                        if (item is ITextItem)
 
106
                                return GetPath (item as ITextItem);
 
107
                        throw new Exception ("Inappropriate Item type.");
 
108
                }
 
109
 
 
110
                protected string GetPath (IFileItem item)
 
111
                {
 
112
                        return item.Path;
 
113
                }
 
114
 
 
115
                protected string GetPath (ITextItem item)
 
116
                {
 
117
                        return item.Text.Replace ("~", 
 
118
                                Environment.GetFolderPath (Environment.SpecialFolder.Personal));
 
119
                }
 
120
                
 
121
                protected string RunProcess (string command, string args) 
 
122
                {
 
123
                        try {
 
124
                                ProcessStartInfo cmd = new ProcessStartInfo ();
 
125
                                cmd.FileName = command;
 
126
                                cmd.Arguments = args; 
 
127
                                cmd.UseShellExecute = false;
 
128
                                cmd.RedirectStandardOutput = true;
 
129
                                
 
130
                                Process ps = Process.Start (cmd);
 
131
                                ps.WaitForExit ();
 
132
                                
 
133
                                string stdout = ps.StandardOutput.ReadLine ();
 
134
                                
 
135
                                if (stdout == null) return string.Empty;
 
136
                                return stdout;
 
137
                                
 
138
                        } catch (Exception e) {
 
139
                                Log<Dropbox>.Error ("Error running {0} {1} : {2}", command, args, e.Message);
 
140
                                Log<Dropbox>.Debug (e.StackTrace);
 
141
                                
 
142
                                return null;
 
143
                        }
 
144
                }
 
145
        }
 
146
}