~cszikszoy/do-plugins/evolution-plugin

« back to all changes in this revision

Viewing changes to Evolution/src/EmailAction.cs

  • Committer: chris at szikszoy
  • Date: 2008-11-28 23:47:59 UTC
  • Revision ID: chris@szikszoy.com-20081128234759-hb62cy5nvfogco95
EmailAction added to Evolution plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// EmailAction.cs created with MonoDevelop
 
2
//
 
3
//  GNOME Do is the legal property of its developers.
 
4
//  Please refer to the COPYRIGHT file distributed with this
 
5
//  source distribution.
 
6
//
 
7
//  This program is free software: you can redistribute it and/or modify
 
8
//  it under the terms of the GNU General Public License as published by
 
9
//  the Free Software Foundation, either version 3 of the License, or
 
10
//  (at your option) any later version.
 
11
//
 
12
//  This program is distributed in the hope that it will be useful,
 
13
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
//  GNU General Public License for more details.
 
16
//
 
17
//  You should have received a copy of the GNU General Public License
 
18
//  along with this program.  If not, see <http://www.gnu.org/licenses/>
 
19
 
 
20
using System;
 
21
using System.Linq;
 
22
using System.Threading;
 
23
using System.Diagnostics;
 
24
using System.Collections.Generic;
 
25
using System.Text.RegularExpressions;
 
26
 
 
27
using Mono.Unix;
 
28
 
 
29
using Do;
 
30
using Do.Universe;
 
31
 
 
32
namespace Evolution
 
33
{
 
34
        
 
35
        public class EmailAction : AbstractAction
 
36
        {
 
37
                public EmailAction()
 
38
                {
 
39
                }
 
40
 
 
41
                public override string Name {
 
42
                        get { return Catalog.GetString("Email with Evolution"); }
 
43
                }
 
44
 
 
45
                public override string Description {
 
46
                        get { return Catalog.GetString("Send an email with Evolution Mail"); }
 
47
                }
 
48
 
 
49
                public override string Icon {
 
50
                        get { return "stock_mail-compose"; }
 
51
                }
 
52
 
 
53
                public override IEnumerable<Type> SupportedItemTypes {
 
54
                        get {
 
55
                                return new Type [] {
 
56
                                        typeof (ContactItem),
 
57
                                        typeof (IContactDetailItem),
 
58
                                        typeof (ITextItem),
 
59
                                };
 
60
                        }
 
61
                }
 
62
 
 
63
                public override IEnumerable<Type> SupportedModifierItemTypes {
 
64
                        get {
 
65
                                return new Type [] {
 
66
                                        typeof (IFileItem),
 
67
                                        typeof (ITextItem),
 
68
                                };
 
69
                        }
 
70
                }
 
71
 
 
72
                public override bool ModifierItemsOptional {
 
73
                        get { return true; }
 
74
                }
 
75
 
 
76
                public override bool SupportsItem (IItem item)
 
77
                {
 
78
                        if (item is ContactItem) {
 
79
                                foreach (string detail in (item as ContactItem).Details) {
 
80
                                        if (detail.StartsWith ("email"))
 
81
                                                return true;
 
82
                                }
 
83
                        } else if (item is IContactDetailItem) {
 
84
                                return (item as IContactDetailItem).Key.StartsWith ("email");
 
85
                        } else if (item is ITextItem) {
 
86
                                return new Regex (
 
87
                                        // Regex should conform to RFC2822.
 
88
                                        @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?"
 
89
                                ).IsMatch ((item as ITextItem).Text);
 
90
                        }
 
91
                        return false;
 
92
                }
 
93
 
 
94
                public override bool SupportsModifierItemForItems (IEnumerable<IItem> items, IItem modItem)
 
95
                {
 
96
                        return true;
 
97
                }
 
98
 
 
99
                public override IEnumerable<IItem> DynamicModifierItemsForItem (IItem item)
 
100
                {
 
101
                        return null;
 
102
                }
 
103
 
 
104
                public override IEnumerable<IItem> Perform (IEnumerable<IItem> items, IEnumerable<IItem> modItems)
 
105
                {
 
106
                        string emails, email, extra;
 
107
                        emails = email = extra = string.Empty;
 
108
                        
 
109
                        foreach (IItem item in items) {
 
110
                                if (item is ContactItem) {
 
111
                                        ContactItem contact = item as ContactItem;
 
112
                                        email = contact ["email"];
 
113
 
 
114
                                        if (email == null) {
 
115
                                                foreach (string detail in contact.Details) {
 
116
                                                        if (detail.StartsWith ("email")) {
 
117
                                                                email = contact [detail];
 
118
                                                                break;
 
119
                                                        }
 
120
                                                }
 
121
                                        }
 
122
                                } else if (item is IContactDetailItem) {
 
123
                                        email = (item as IContactDetailItem).Value;
 
124
                                } else if (item is ITextItem) {
 
125
                                        email = (item as ITextItem).Text;
 
126
                                }
 
127
                                emails += email + ",";
 
128
                        }
 
129
 
 
130
                        if (modItems.Any ()) {
 
131
                                if (modItems.First () is ITextItem)
 
132
                                        extra = "?body=" + (modItems.First () as ITextItem).Text
 
133
                                                .Replace ("\"", "\\\""); // Try to escape quotes...
 
134
                                else if (modItems.First () is IFileItem)
 
135
                                        extra = "?attach='" + (modItems.First () as IFileItem).Path + "'";
 
136
                        }               
 
137
                        
 
138
                        Thread send = new Thread ( new ParameterizedThreadStart (SendEmail));
 
139
                        send.Start (string.Format ("mailto:{0}{1}", emails, extra));
 
140
 
 
141
                        return null;
 
142
                }
 
143
                
 
144
                private void SendEmail(object arguments)
 
145
                {
 
146
                        ProcessStartInfo send_email;
 
147
                        send_email = new ProcessStartInfo ("evolution", arguments.ToString());
 
148
                        send_email.UseShellExecute = false;
 
149
                        send_email.RedirectStandardOutput = true;
 
150
                        send_email.RedirectStandardError = true;
 
151
                        //use Logging class to output anything from stdout / stderr
 
152
                        
 
153
                        using (Process task = Process.Start (send_email))
 
154
                                task.WaitForExit();                     
 
155
                }
 
156
        }
 
157
}