~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

« back to all changes in this revision

Viewing changes to Do.Addins/src/Do.Universe/EmailAction.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-09-14 10:09:40 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20080914100940-kyghudg7py14bu2z
Tags: upstream-0.6.0.0
ImportĀ upstreamĀ versionĀ 0.6.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// EmailAction.cs
 
2
// 
 
3
// GNOME Do is the legal property of its developers, whose names are too
 
4
// numerous to list here.  Please refer to the COPYRIGHT file distributed with
 
5
// this source distribution.
 
6
// 
 
7
// This program is free software: you can redistribute it and/or modify it under
 
8
// the terms of the GNU General Public License as published by the Free Software
 
9
// Foundation, either version 3 of the License, or (at your option) any later
 
10
// version.
 
11
// 
 
12
// This program is distributed in the hope that it will be useful, but WITHOUT
 
13
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
15
// details.
 
16
// 
 
17
// You should have received a copy of the GNU General Public License along with
 
18
// this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
using System;
 
21
using System.Text.RegularExpressions;
 
22
using Mono.Unix;
 
23
 
 
24
using Do.Addins;
 
25
using Do.Universe;
 
26
 
 
27
namespace Do.Universe
 
28
{
 
29
        public class EmailAction : AbstractAction
 
30
        {
 
31
                public override string Name {
 
32
                        get {
 
33
                                return Catalog.GetString ("Email");
 
34
                        }
 
35
                }
 
36
 
 
37
                public override string Description {
 
38
                        get {
 
39
                                return Catalog.GetString ("Compose a new email to a friend.");
 
40
                        }
 
41
                }
 
42
 
 
43
                public override string Icon {
 
44
                        get { return "stock_mail-compose"; }
 
45
                }
 
46
 
 
47
                public override Type [] SupportedItemTypes {
 
48
                        get {
 
49
                                return new Type [] {
 
50
                                        typeof (ContactItem),
 
51
                                        typeof (IContactDetailItem),
 
52
                                        typeof (ITextItem),
 
53
                                };
 
54
                        }
 
55
                }
 
56
 
 
57
                public override Type [] SupportedModifierItemTypes {
 
58
                        get {
 
59
                                return new Type [] {
 
60
                                        typeof (ITextItem),
 
61
                                };
 
62
                        }
 
63
                }
 
64
 
 
65
                public override bool ModifierItemsOptional {
 
66
                        get { return true; }
 
67
                }
 
68
 
 
69
                public override bool SupportsItem (IItem item)
 
70
                {
 
71
                        if (item is ContactItem) {
 
72
                                foreach (string detail in (item as ContactItem).Details) {
 
73
                                        if (detail.StartsWith ("email"))
 
74
                                                return true;
 
75
                                }
 
76
                        } else if (item is IContactDetailItem) {
 
77
                                return (item as IContactDetailItem).Key.StartsWith ("email");
 
78
                        } else if (item is ITextItem) {
 
79
                                return new Regex (
 
80
                                        // Regex should conform to RFC2822.
 
81
                                        @"[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])?"
 
82
                                ).IsMatch ((item as ITextItem).Text);
 
83
                        }
 
84
                        return false;
 
85
                }
 
86
 
 
87
                public override IItem [] Perform (IItem [] items, IItem [] modItems)
 
88
                {
 
89
                        string emails, email, body;
 
90
 
 
91
                        emails = email = string.Empty;
 
92
                        foreach (IItem item in items) {
 
93
                                if (item is ContactItem) {
 
94
                                        ContactItem contact = item as ContactItem;
 
95
                                        email = contact ["email"];
 
96
 
 
97
                                        if (email == null) {
 
98
                                                foreach (string detail in contact.Details) {
 
99
                                                        if (detail.StartsWith ("email")) {
 
100
                                                                email = contact [detail];
 
101
                                                                break;
 
102
                                                        }
 
103
                                                }
 
104
                                        }
 
105
                                } else if (item is IContactDetailItem) {
 
106
                                        email = (item as IContactDetailItem).Value;
 
107
                                } else if (item is ITextItem) {
 
108
                                        email = (item as ITextItem).Text;
 
109
                                }
 
110
                                emails += email + ",";
 
111
                        }
 
112
 
 
113
                        body = string.Empty;
 
114
                        if (modItems.Length > 0) {
 
115
                                body = "?body=" + (modItems [0] as ITextItem).Text
 
116
                                        .Replace ("\"", "\\\""); // Try to escape quotes...
 
117
                        }
 
118
                        Util.Environment.Open ("\"mailto:" + emails + body + "\"");
 
119
                        return null;
 
120
                }
 
121
        }
 
122
}