~do-core/do/trunk

« back to all changes in this revision

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

  • Committer: David Siegel
  • Date: 2008-11-23 21:51:31 UTC
  • Revision ID: david@dell-desktop-20081123215131-kfohdq0ntipk782p
Restructured codebase to introduce a new layer of organization, and to begin mass refactorization of Do.Addins (and core) into separate assemblies.

        * Do.Platform: platform-neutral codebase to be used by non-interface plugins and core. Plugins that only use code from this assembly are cross-platform.
                * Do.Platform.UniverseFactory: create abstract items, like Applications (IApplicationItem) and Files (IFileItem).
                * Do.Platform.*: Platform-neutral logging, preferences, other utilities.

        * Do.Platform.Linux: Linux-specific implementations to be used by classes in Do.Platform. Plugins that use code from this assembly are Linux-dependent.

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.Linq;
22
 
using System.Collections.Generic;
23
 
using System.Text.RegularExpressions;
24
 
 
25
 
using Mono.Unix;
26
 
 
27
 
using Do.Addins;
28
 
using Do.Universe;
29
 
 
30
 
namespace Do.Universe
31
 
{
32
 
        public class EmailAction : AbstractAction
33
 
        {
34
 
                public override string Name {
35
 
                        get {
36
 
                                return Catalog.GetString ("Email");
37
 
                        }
38
 
                }
39
 
 
40
 
                public override string Description {
41
 
                        get {
42
 
                                return Catalog.GetString ("Compose a new email to a friend.");
43
 
                        }
44
 
                }
45
 
 
46
 
                public override string Icon {
47
 
                        get { return "stock_mail-compose"; }
48
 
                }
49
 
 
50
 
                public override IEnumerable<Type> SupportedItemTypes {
51
 
                        get {
52
 
                                return new Type [] {
53
 
                                        typeof (ContactItem),
54
 
                                        typeof (IContactDetailItem),
55
 
                                        typeof (ITextItem),
56
 
                                };
57
 
                        }
58
 
                }
59
 
 
60
 
                public override IEnumerable<Type> SupportedModifierItemTypes {
61
 
                        get {
62
 
                                yield return typeof (ITextItem);
63
 
                        }
64
 
                }
65
 
 
66
 
                public override bool ModifierItemsOptional {
67
 
                        get { return true; }
68
 
                }
69
 
 
70
 
                public override bool SupportsItem (IItem item)
71
 
                {
72
 
                        if (item is ContactItem) {
73
 
                                foreach (string detail in (item as ContactItem).Details) {
74
 
                                        if (detail.StartsWith ("email"))
75
 
                                                return true;
76
 
                                }
77
 
                        } else if (item is IContactDetailItem) {
78
 
                                return (item as IContactDetailItem).Key.StartsWith ("email");
79
 
                        } else if (item is ITextItem) {
80
 
                                return new Regex (
81
 
                                        // Regex should conform to RFC2822.
82
 
                                        @"[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])?"
83
 
                                ).IsMatch ((item as ITextItem).Text);
84
 
                        }
85
 
                        return false;
86
 
                }
87
 
 
88
 
                public override IEnumerable<IItem> Perform (IEnumerable<IItem> items, IEnumerable<IItem> modItems)
89
 
                {
90
 
                        string emails, email, body;
91
 
 
92
 
                        emails = email = string.Empty;
93
 
                        foreach (IItem item in items) {
94
 
                                if (item is ContactItem) {
95
 
                                        ContactItem contact = item as ContactItem;
96
 
                                        email = contact ["email"];
97
 
 
98
 
                                        if (email == null) {
99
 
                                                foreach (string detail in contact.Details) {
100
 
                                                        if (detail.StartsWith ("email")) {
101
 
                                                                email = contact [detail];
102
 
                                                                break;
103
 
                                                        }
104
 
                                                }
105
 
                                        }
106
 
                                } else if (item is IContactDetailItem) {
107
 
                                        email = (item as IContactDetailItem).Value;
108
 
                                } else if (item is ITextItem) {
109
 
                                        email = (item as ITextItem).Text;
110
 
                                }
111
 
                                emails += email + ",";
112
 
                        }
113
 
 
114
 
                        body = string.Empty;
115
 
                        if (modItems.Any ()) {
116
 
                                body = "?body=" + (modItems.First () as ITextItem).Text
117
 
                                        .Replace ("\"", "\\\""); // Try to escape quotes...
118
 
                        }
119
 
                        Util.Environment.Open ("\"mailto:" + emails + body + "\"");
120
 
                        return null;
121
 
                }
122
 
        }
123
 
}