~cszikszoy/do-plugins/fix-confluence

« back to all changes in this revision

Viewing changes to Quote/src/QuoteAction.cs

  • Committer: David Siegel
  • Date: 2009-01-07 19:40:53 UTC
  • mfrom: (407.1.4 trunk)
  • Revision ID: david@x-20090107194053-wseaq4rkw4y12bka
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* QuoteAction.cs 
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * 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
 
 
21
using System;
 
22
using System.Linq;
 
23
using System.Collections.Generic;
 
24
using System.Text.RegularExpressions;
 
25
 
 
26
using Do.Platform;
 
27
using Do.Universe;
 
28
using Do.Universe.Common;
 
29
 
 
30
using Mono.Unix;
 
31
 
 
32
namespace Quote
 
33
{
 
34
        public class PostQuote : Act
 
35
        {
 
36
                // thanks Ian Warford (iwarford) for the regexp help
 
37
                const string TimeStampRegexp = @"\n\s*\S?\d\d:\d\d(:\d\d)?\S?\s*|^\s*\S?\d\d:\d\d(:\d\d)?\S?\s*";
 
38
                
 
39
                public override string Name {
 
40
                        get { return Catalog.GetString ("Submit Quote"); }
 
41
                }
 
42
                
 
43
                public override string Description {
 
44
                        get { return Catalog.GetString ("Sends text to Quote service."); }
 
45
                }
 
46
                
 
47
                public override string Icon {
 
48
                        get { return "quoted-globe.svg@" + GetType ().Assembly.FullName; }
 
49
                }
 
50
                
 
51
                public override IEnumerable<Type> SupportedItemTypes {
 
52
                        get { yield return typeof (ITextItem); }
 
53
                }
 
54
                
 
55
                public override bool ModifierItemsOptional  {
 
56
                        get { return true; }
 
57
                }
 
58
                
 
59
                public override IEnumerable<Type> SupportedModifierItemTypes {
 
60
                        get { 
 
61
                                yield return typeof (ITextItem);
 
62
                                yield return typeof (QuoteTagItem);
 
63
                        }
 
64
                }
 
65
                
 
66
                public override IEnumerable<Item> DynamicModifierItemsForItem (Item item)
 
67
                {
 
68
                        IQuoteProvider quoteProvider = QuoteProviderFactory.GetProviderFromPreferences ();
 
69
                
 
70
                        return quoteProvider.SavedTags.Cast<Item> ();
 
71
                }
 
72
                
 
73
                public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
 
74
                {
 
75
                        Regex timestamps = new Regex (TimeStampRegexp, RegexOptions.Compiled);
 
76
                        
 
77
                        string text;
 
78
                        string tags = "";
 
79
                        IQuoteProvider quoteProvider;
 
80
                
 
81
                        text = (items.First () as ITextItem).Text;
 
82
                        text = timestamps.Replace (text, "\n");
 
83
 
 
84
                        Console.Error.WriteLine (text);
 
85
                
 
86
                        foreach (Item tag in modifierItems) {
 
87
                                tags += tag is QuoteTagItem
 
88
                                        ? (tag as QuoteTagItem).Name
 
89
                                        : (tag as ITextItem).Text;
 
90
                
 
91
                                tags += " ";
 
92
                        }
 
93
                
 
94
                        quoteProvider = string.IsNullOrEmpty (tags)
 
95
                                ? QuoteProviderFactory.GetProviderFromPreferences (text)
 
96
                                : QuoteProviderFactory.GetProviderFromPreferences (text, tags);         
 
97
                
 
98
                        string url = Quote.PostUsing (quoteProvider);
 
99
                
 
100
                        AddUnknownTags (tags, quoteProvider);
 
101
                
 
102
                        yield return new BookmarkItem (url, url);
 
103
                }
 
104
                
 
105
                void AddUnknownTags (string tags, IQuoteProvider service)
 
106
                {
 
107
                        QuoteTagItem tag;
 
108
                
 
109
                        foreach (string tagName in tags.Trim ().Split (' ')) {
 
110
                                tag = new QuoteTagItem (tagName);
 
111
                
 
112
                                if (service.SavedTags.Contains (tag)) continue;
 
113
                                
 
114
                                service.AddTag (tag);
 
115
                        }
 
116
                }
 
117
        }
 
118
}
 
119