~cszikszoy/do-plugins/fix-confluence

« back to all changes in this revision

Viewing changes to Confluence/src/ConfluenceConfiguration.cs

  • Committer: Chris
  • Date: 2009-06-20 02:11:31 UTC
  • Revision ID: chris@szikszoy.com-20090620021131-cahieuyuq6zusq47
fix confluence shit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// This program is free software; you can redistribute it and/or modify
3
 
// it under the terms of the GNU General Public License as published by
4
 
// the Free Software Foundation; either version 3 of the License, or
5
 
// (at your option) any later version.
6
 
//
7
 
// This program is distributed in the hope that it will be useful,
8
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
 
// GNU General Public License for more details.
11
 
//
12
 
// You should have received a copy of the GNU General Public License
13
 
// along with this program; if not, write to the Free Software
14
 
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
 
//
16
 
 
17
 
using System;
18
 
 
19
 
 
20
 
namespace Confluence
21
 
{
22
 
        public interface IConfluenceConfiguration
23
 
        {
24
 
                string BaseUrl 
25
 
                {
26
 
                        get;
27
 
                }
28
 
                
29
 
                string Username
30
 
                {
31
 
                        get;
32
 
                }
33
 
                
34
 
                string Password 
35
 
                {
36
 
                        get;
37
 
                }
38
 
                
39
 
                int MaxSearchResults
40
 
                {
41
 
                        get;
42
 
                }
43
 
                
44
 
                /// <value>
45
 
                /// To be valid, all required fields must be filled out
46
 
                /// </value>
47
 
                bool IsValid();
48
 
        }       
49
 
        
50
 
        /// <summary>
51
 
        /// Configuration that uses gconf to do the magic
52
 
        /// </summary>
53
 
        public class ConfluenceConfiguration : IConfluenceConfiguration
54
 
        {
55
 
                /// <summary>
56
 
                /// The gnome conf key that contains the base url for our Confluence installation.
57
 
                /// ie. http://opensource.atlassian.com/confluence/spring
58
 
                /// </summary>
59
 
                private const string _sGCONF_KEY= "/apps/gnome-do/plugins/Confluence";
60
 
                
61
 
                private GConf.Client _gconf;
62
 
                
63
 
                public ConfluenceConfiguration()
64
 
                {
65
 
                        _gconf= new GConf.Client();
66
 
                        
67
 
                        // Set defaults
68
 
                        MaxSearchResults = DefaultMaxSearchResults;
69
 
                }
70
 
                
71
 
                private void SetConfValue( string key, string val )
72
 
                {
73
 
                        _gconf.Set( _sGCONF_KEY + "/" + key, val );
74
 
                }
75
 
                
76
 
                private string GetConfValue( string key, string fallback )
77
 
                {
78
 
                        try
79
 
                        {
80
 
                                return _gconf.Get( _sGCONF_KEY + "/" + key ) as String;
81
 
                        }
82
 
                        catch( GConf.NoSuchKeyException ) 
83
 
                        {
84
 
                                return fallback;
85
 
                        }
86
 
                }
87
 
                
88
 
                public string BaseUrl
89
 
                {
90
 
                        get { return GetConfValue( "baseUrl", "" ); }
91
 
                        set 
92
 
                        {
93
 
                                value = value.Trim();
94
 
                                if (value.EndsWith("/"))
95
 
                                {
96
 
                                        char[] trimChars = { '/' };
97
 
                                        value = value.TrimEnd(trimChars);
98
 
                                }
99
 
                                        
100
 
                                SetConfValue( "baseUrl", value ); 
101
 
                        }
102
 
                }
103
 
                
104
 
                public string Username
105
 
                {
106
 
                        get { return GetConfValue( "username", "" ); }
107
 
                        set { SetConfValue( "username", value ); }
108
 
                }
109
 
                
110
 
                public string Password 
111
 
                {
112
 
                        get { return GetConfValue( "password", "" ); }
113
 
                        set { SetConfValue( "password", value ); }
114
 
                }
115
 
                
116
 
                public int MaxSearchResults
117
 
                {
118
 
                        get 
119
 
                        {
120
 
                                try 
121
 
                                {
122
 
                                        return Convert.ToInt32(GetConfValue( "maxSearchResults", "" ));
123
 
                                } 
124
 
                                catch (FormatException) 
125
 
                                {
126
 
                                        return DefaultMaxSearchResults;
127
 
                                }
128
 
                        }
129
 
                        
130
 
                        set { SetConfValue( "maxSearchResults", Convert.ToString( value )); }
131
 
                }
132
 
                
133
 
                public int DefaultMaxSearchResults
134
 
                {
135
 
                        get 
136
 
                        { 
137
 
                                return 20; 
138
 
                        }
139
 
                }
140
 
                
141
 
                /// <value>
142
 
                /// To be valid, all required fields must be filled out
143
 
                /// </value>
144
 
                public bool IsValid()
145
 
                {
146
 
                        return BaseUrl.Length>0;
147
 
                }
148
 
        }
149
 
}