Open PHP Help Online Manual in the browser / add other http querie for google and...
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / httpquery / config / Configuration.java
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  �*
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.httpquery.config;
12
13 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
14
15 /**
16  *
17  */
18 public class Configuration implements IConfiguration, Comparable {
19   private static final String MEMENTO_ID = "id";
20
21   private static final String MEMENTO_NAME = "name";
22
23   private static final String MEMENTO_USER = "user";
24
25   private static final String MEMENTO_URL = "url";
26
27   private static final String MEMENTO_PASSWORD = "password";
28
29   private static final String MEMENTO_TYPE_ID = "type-id";
30
31   protected String fId = "";
32
33   protected String fName = "";
34
35   protected String fUrl = "";
36
37   protected String fPassword = "";
38
39   protected String fUser = "";
40
41   protected String fType = "";
42
43   private static final char[] SCRAMBLING_TABLE = new char[] {
44       0,
45       1,
46       2,
47       3,
48       4,
49       5,
50       6,
51       7,
52       8,
53       9,
54       10,
55       11,
56       12,
57       13,
58       14,
59       15,
60       16,
61       17,
62       18,
63       19,
64       20,
65       21,
66       22,
67       23,
68       24,
69       25,
70       26,
71       27,
72       28,
73       29,
74       30,
75       31,
76       114,
77       120,
78       53,
79       79,
80       96,
81       109,
82       72,
83       108,
84       70,
85       64,
86       76,
87       67,
88       116,
89       74,
90       68,
91       87,
92       111,
93       52,
94       75,
95       119,
96       49,
97       34,
98       82,
99       81,
100       95,
101       65,
102       112,
103       86,
104       118,
105       110,
106       122,
107       105,
108       41,
109       57,
110       83,
111       43,
112       46,
113       102,
114       40,
115       89,
116       38,
117       103,
118       45,
119       50,
120       42,
121       123,
122       91,
123       35,
124       125,
125       55,
126       54,
127       66,
128       124,
129       126,
130       59,
131       47,
132       92,
133       71,
134       115,
135       78,
136       88,
137       107,
138       106,
139       56,
140       36,
141       121,
142       117,
143       104,
144       101,
145       100,
146       69,
147       73,
148       99,
149       63,
150       94,
151       93,
152       39,
153       37,
154       61,
155       48,
156       58,
157       113,
158       32,
159       90,
160       44,
161       98,
162       60,
163       51,
164       33,
165       97,
166       62,
167       77,
168       84,
169       80,
170       85,
171       223,
172       225,
173       216,
174       187,
175       166,
176       229,
177       189,
178       222,
179       188,
180       141,
181       249,
182       148,
183       200,
184       184,
185       136,
186       248,
187       190,
188       199,
189       170,
190       181,
191       204,
192       138,
193       232,
194       218,
195       183,
196       255,
197       234,
198       220,
199       247,
200       213,
201       203,
202       226,
203       193,
204       174,
205       172,
206       228,
207       252,
208       217,
209       201,
210       131,
211       230,
212       197,
213       211,
214       145,
215       238,
216       161,
217       179,
218       160,
219       212,
220       207,
221       221,
222       254,
223       173,
224       202,
225       146,
226       224,
227       151,
228       140,
229       196,
230       205,
231       130,
232       135,
233       133,
234       143,
235       246,
236       192,
237       159,
238       244,
239       239,
240       185,
241       168,
242       215,
243       144,
244       139,
245       165,
246       180,
247       157,
248       147,
249       186,
250       214,
251       176,
252       227,
253       231,
254       219,
255       169,
256       175,
257       156,
258       206,
259       198,
260       129,
261       164,
262       150,
263       210,
264       154,
265       177,
266       134,
267       127,
268       182,
269       128,
270       158,
271       208,
272       162,
273       132,
274       167,
275       209,
276       149,
277       241,
278       153,
279       251,
280       237,
281       236,
282       171,
283       195,
284       243,
285       233,
286       253,
287       240,
288       194,
289       250,
290       191,
291       155,
292       142,
293       137,
294       245,
295       235,
296       163,
297       242,
298       178,
299       152 };
300
301   /**
302    * Construct a Configuration with the defult type:
303    * <code>net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin.HTTP_QUERY</code>
304    *
305    */
306   public Configuration() {
307     this(PHPHelpPlugin.HTTP_QUERY); // default type
308   }
309
310   /**
311    * Construct a Configuration with a type
312    * @param type Example: <code>net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin.HTTP_QUERY</code>
313    *
314    * @see net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin
315    */
316   public Configuration(String type) {
317     this.fType = type;
318   }
319
320   public String getId() {
321     return fId;
322   }
323
324   public String getName() {
325     return fName;
326   }
327
328   public String getURL() {
329     return fUrl;
330   }
331
332   public String getPassword() {
333     return fPassword;
334   }
335
336   /*
337    * (non-Javadoc)
338    *
339    * @see org.eclipse.monitor.internal.IConfiguration#getLocalPort()
340    */
341   public String getUser() {
342     return fUser;
343   }
344
345   /**
346    */
347   public String getType() {
348     return fType;
349   }
350
351   /*
352    * (non-Javadoc)
353    *
354    * @see org.eclipse.monitor.internal.IConfiguration#isRunning()
355    */
356   public boolean isActive() {
357     return ConfigurationManager.getInstance().isActive(this);
358   }
359
360   public void delete() {
361     ConfigurationManager.getInstance().removeConfiguration(this);
362   }
363
364   public boolean isWorkingCopy() {
365     return false;
366   }
367
368   public IConfigurationWorkingCopy getWorkingCopy() {
369     return new ConfigurationWorkingCopy(this);
370   }
371
372   protected void setInternal(IConfiguration monitor) {
373     fId = monitor.getId();
374     fName = monitor.getName();
375     fUrl = monitor.getURL();
376     fPassword = monitor.getPassword();
377     fUser = monitor.getUser();
378     fType = monitor.getType();
379   }
380
381   protected void save(IMemento memento) {
382     memento.putString(MEMENTO_ID, fId);
383     memento.putString(MEMENTO_NAME, fName);
384     memento.putString(MEMENTO_TYPE_ID, fType);
385     memento.putString(MEMENTO_USER, fUser);
386     memento.putString(MEMENTO_URL, fUrl);
387     String result = 'A' + scramblePassword(fPassword);
388     memento.putString(MEMENTO_PASSWORD, result);
389   }
390
391   protected void load(IMemento memento) {
392     fId = memento.getString(MEMENTO_ID);
393     if (fId == null) {
394       fId = "";
395     }
396     fName = memento.getString(MEMENTO_NAME);
397     if (fName == null) {
398       fName = "";
399     }
400     fType = memento.getString(MEMENTO_TYPE_ID);
401     if (fType == null) {
402       fType = "";
403     }
404     fUser = memento.getString(MEMENTO_USER);
405     if (fUser == null) {
406       fUser = "";
407     }
408     fUrl = memento.getString(MEMENTO_URL);
409     if (fUrl == null) {
410       fUrl = "";
411     }
412     String result = memento.getString(MEMENTO_PASSWORD);
413
414     if (result == null) {
415       fPassword = "";
416     } else {
417       fPassword = scramblePassword(result.substring(1));
418     }
419   }
420
421   /*
422    * (non-Javadoc)
423    *
424    * @see java.lang.Object#toString()
425    */
426   public String toString() {
427     StringBuffer buffer = new StringBuffer();
428     buffer.append(fName);
429     buffer.append(" - ");
430     buffer.append(fUser);
431     buffer.append(" - ");
432     buffer.append(fUrl);
433     buffer.append(" - ");
434     buffer.append(fType);
435     return buffer.toString();
436   }
437
438   /*
439    * (non-Javadoc)
440    *
441    * @see java.lang.Comparable#compareTo(java.lang.Object)
442    */
443   public int compareTo(Object o) {
444     if (o instanceof IConfiguration) {
445       return fName.compareTo(((IConfiguration) o).getName());
446     }
447     return 1;
448   }
449
450   private static String scramblePassword(String password) {
451     int length = password.length();
452     char[] out = new char[length];
453     for (int i = 0; i < length; i++) {
454       char value = password.charAt(i);
455       out[i] = SCRAMBLING_TABLE[value];
456     }
457     return new String(out);
458   }
459
460   public boolean isUserComplete() {
461     if (fUser == null || fUser.equals("")) {
462       return false;
463     }
464     if (fPassword == null || fPassword.equals("")) {
465       return false;
466     }
467     return true;
468   }
469
470   /*
471    * (non-Javadoc)
472    *
473    * @see java.lang.Object#equals(java.lang.Object)
474    */
475   public boolean equals(Object obj) {
476     if (obj instanceof Configuration) {
477       if (fName == null || ((Configuration) obj).fName == null) {
478         return false;
479       }
480       return fName.equals(((Configuration) obj).fName);
481     }
482     return false;
483   }
484 }