Skip to main content
summaryrefslogtreecommitdiffstats
blob: 093b2aadba989ff8e4687a8fece9bf3cf150e0d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*******************************************************************************
 * Copyright (c) 2003 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.bugzilla.core;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.mylar.bugzilla.core.internal.ProductConfiguration;
import org.eclipse.mylar.bugzilla.core.internal.ProductConfigurationFactory;
import org.eclipse.mylar.bugzilla.core.search.BugzillaQueryPageParser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;


/**
 * @version 	1.0
 * @author		G. Murphy
 */

/**
 * The class that deals with the preferences page in eclipse
 */
public class BugzillaPreferences
	extends FieldEditorPreferencePage
	implements IWorkbenchPreferencePage {

	/** Secure http server prefix */
	private static final String httpsPrefix = "https://";

	/** http prefix */
	private static final String httpPrefix = "http://";

	/** The text to put into the label for the bugzilla server text box */
	private static final String bugzillaServerLabel = "Bugzilla Server: ";

	/** Field editor for the bugzilla server in the preferences page */
	private StringFieldEditor bugzillaServer;

	private static final String bugzillaUserLabel = "Bugzilla User Name: ";

	private static final String bugzillaPasswordLabel = "Bugzilla Password: ";


    private static final String bugzilla220Label = "Using Bugzilla 2.20 compatiblity (overrides option below, some features disabled)";
    private static final String bugzilla218Label = "Using Bugzilla 2.18 (default is 2.16)";
        
    private static final String bugzillaMaxResultsLabel = "Maximum returned results: ";
    
    private BooleanFieldEditor bugzilla220;
    private BooleanFieldEditor bugzilla218;
    
	private StringFieldEditor bugzillaUser;

	private MyStringFieldEditor bugzillaPassword;
	
	private IntegerFieldEditor maxResults;
	
	private BooleanFieldEditor refreshQueries;

	/**
	 * Constructor for the preferences page
	 */
	public BugzillaPreferences() {
		super(GRID);

		// set the preference store for this preference page
		setPreferenceStore(BugzillaPlugin.getDefault().getPreferenceStore());
	}

	@Override
	public void createControl(Composite parent) {
		super.createControl(parent);		
	}

	@Override
	protected void createFieldEditors() {
		// create a new field editor for the bugzilla server	
		bugzillaServer = new StringFieldEditor(
				IBugzillaConstants.BUGZILLA_SERVER, bugzillaServerLabel,
				StringFieldEditor.UNLIMITED, getFieldEditorParent()) {
			
			@Override
			protected boolean doCheckState() {
				return checkServerName(getStringValue());
			}
		};

		// set the error message for if the server name check fails
		bugzillaServer.setErrorMessage(
			"Server path must be a valid http(s):// url");

		bugzillaUser = new StringFieldEditor("", bugzillaUserLabel,
				StringFieldEditor.UNLIMITED, getFieldEditorParent());
		bugzillaPassword = new MyStringFieldEditor("", bugzillaPasswordLabel,
				StringFieldEditor.UNLIMITED, getFieldEditorParent());
		bugzillaPassword.getTextControl().setEchoChar('*');

		maxResults = new IntegerFieldEditor(IBugzillaConstants.MAX_RESULTS, bugzillaMaxResultsLabel, getFieldEditorParent());
		
		bugzilla220 = new BooleanFieldEditor(IBugzillaConstants.IS_220, bugzilla220Label, BooleanFieldEditor.DEFAULT, getFieldEditorParent());
		bugzilla218 = new BooleanFieldEditor(IBugzillaConstants.IS_218, bugzilla218Label, BooleanFieldEditor.DEFAULT, getFieldEditorParent());
        
		refreshQueries = new BooleanFieldEditor(IBugzillaConstants.REFRESH_QUERY, "Automatically refresh Bugzilla reports and queries on startup", 
				BooleanFieldEditor.DEFAULT, getFieldEditorParent());
		
		// add the field editor to the preferences page
		addField(bugzillaServer);
		addField(bugzillaUser);
		addField(bugzillaPassword);
		addField(maxResults);
		addField(bugzilla220);
		addField(bugzilla218);
		addField(refreshQueries);
		

		// put the password and user name values into the field editors
		getCachedData();
		bugzillaUser.setStringValue(user);
		bugzillaPassword.setStringValue(password);
	}
    
	/**
	 * Initialize the preferences page with the default values
	 * 
	 * @param store The preferences store that is used to store the information about the preferences page 
	 */
	public static void initDefaults(IPreferenceStore store) {
		// set the default values for the bugzilla server and the 
		// most recent query
		getCachedData();

		store.setDefault(IBugzillaConstants.BUGZILLA_SERVER,IBugzillaConstants.DEFAULT_BUGZILLA_SERVER);
		store.setDefault(IBugzillaConstants.MOST_RECENT_QUERY, "");
		store.setDefault(IBugzillaConstants.IS_218, true);
		store.setDefault(IBugzillaConstants.REFRESH_QUERY, false);
		store.setDefault(IBugzillaConstants.MAX_RESULTS, 100);
        
		// set the default query options for the bugzilla search
		setDefaultQueryOptions();
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		
		/*
		 * set user and password to the new default values
		 * and then give these values to storeCache() to update the keyring
		 */
		user = bugzillaUser.getStringValue();
		password = bugzillaPassword.getStringValue();
		storeCache(user, password, true);
	}

	@Override
	public boolean performOk() {
	    BugzillaPlugin.getDefault().getPreferenceStore().setValue(IBugzillaConstants.IS_218, bugzilla218.getBooleanValue());
	    BugzillaPlugin.getDefault().getPreferenceStore().setValue(IBugzillaConstants.REFRESH_QUERY, refreshQueries.getBooleanValue());
	    BugzillaPlugin.getDefault().getPreferenceStore().setValue(IBugzillaConstants.MAX_RESULTS, maxResults.getIntValue());
		String oldBugzillaServer = BugzillaPlugin.getDefault().getServerName();
		ProductConfiguration configuration = null;
		
		try {
			
			// append "/show_bug.cgi" to url provided for cases where the connection is successful,  
			// but full path hasn't been specified  (i.e. http://hipikat.cs.ubc.ca:8081)
			URL serverURL = new URL(bugzillaServer.getStringValue() + "/show_bug.cgi");
 	
			URLConnection cntx = BugzillaPlugin.getDefault().getUrlConnection(serverURL);
			if(cntx == null || !(cntx instanceof HttpURLConnection))
				return false;
			
			HttpURLConnection serverConnection = (HttpURLConnection)cntx;
			
			serverConnection.connect();	
			
			int responseCode = serverConnection.getResponseCode();
	
			if(responseCode != HttpURLConnection.HTTP_OK)
				throw new BugzillaException("No Bugzilla server detected at " + bugzillaServer.getStringValue() + ".");

			try {
				configuration = ProductConfigurationFactory.getInstance().getConfiguration(bugzillaServer.getStringValue());
			} catch (IOException ex) {
				MessageDialog.openInformation(null, "Bugzilla query parameters check", 
						"An error occurred while pre-fetching valid search attributes: \n\n" +
						ex.getClass().getName() + ": " + ex.getMessage() + 
						"\n\nOffline submission of new bugs will be disabled.");
			}
		}
				
		catch (Exception e) {
			if (!MessageDialog.openQuestion(null, "Bugzilla Server Error", "Error validating Bugzilla Server.\n\n"
											+ e.getMessage()
											+ "\n\nKeep specified server location anyway?"))
			{
				bugzillaServer.setStringValue(oldBugzillaServer);
				return false;
			}
		}

		// save the preferences that were changed
		//BugzillaPlugin.getDefault().savePluginPreferences();

		bugzillaServer.store();

		// store the username and password from the editor field
		user = bugzillaUser.getStringValue();
		password = bugzillaPassword.getStringValue();
		storeCache(user, password, true);

		BugzillaPlugin.getDefault().setProductConfiguration(configuration);
		IPath configFile = BugzillaPlugin.getDefault().getProductConfigurationCachePath();
		if (configuration != null) {
			
			try {
				ProductConfigurationFactory.getInstance().writeConfiguration(configuration, configFile.toFile());
			} catch (IOException e) {
				BugzillaPlugin.log(e);
				configFile.toFile().delete();
			}
		}
		else {
			configFile.toFile().delete();
		}
		return true;
	}

	@Override
	public boolean performCancel() {
//		refreshQueries.setSelection(getPreferenceStore().getBoolean(MylarTasksPlugin.REFRESH_QUERIES));
		return true;
	}
	/**
	 * Determine if the name starts with https:// or http://
	 * 
	 * @param name The string that needs to be checked
	 * @return <code>true</code> if the name starts with https:// or http://, otherwise <code>false</code>
	 */
	private boolean checkServerName(String name) {
		if (name.startsWith(httpsPrefix) || name.startsWith(httpPrefix))
			return true;
		return false;
	}

	@Override
	protected void initialize() {
		super.initialize();

		// put the password and user name values into the field editors
		getCachedData();
		bugzillaUser.setStringValue(user);
		bugzillaPassword.setStringValue(password);
	}

	public void init(IWorkbench workbench) {
		// Don't need to do anything here with the workbench
	}

	/**
	 * Hack private class to make StringFieldEditor.refreshValidState()
	 * a publicly acessible method.
	 * 
	 * @see org.eclipse.jface.preference.StringFieldEditor#refreshValidState()
	 */
	private static class MyStringFieldEditor extends StringFieldEditor {
		public MyStringFieldEditor(String name, String labelText, int style,
				Composite parent) {
			super(name, labelText, style, parent);
		}
		
		@Override
		public void refreshValidState() {
			super.refreshValidState();
		}

		@Override
		public Text getTextControl() {
			return super.getTextControl();
		}

	}

	/**
	 * Update all of the query options for the bugzilla search page
	 * 
	 * @param monitor A reference to a progress monitor
	 */
	public static void updateQueryOptions(IProgressMonitor monitor)
			throws LoginException {
		// make a new page parser so that we can get the information from the server
		BugzillaQueryPageParser parser = new BugzillaQueryPageParser(monitor);
		if (!parser.wasSuccessful())
			return;

		// get the preferences store so that we can change the data in it	
		IPreferenceStore prefs = BugzillaPlugin.getDefault().getPreferenceStore();

		// get the new values for the status field and increment the status monitor
		prefs.setValue(IBugzillaConstants.STATUS_VALUES,
			queryOptionsToString(parser.getStatusValues()));
		monitor.worked(1);

		// get the new values for the preselected status values and increment the status monitor
		prefs.setValue(IBugzillaConstants.PRESELECTED_STATUS_VALUES,
			queryOptionsToString(parser.getPreselectedStatusValues()));
		monitor.worked(1);

		// get the new values for the resolution field and increment the status monitor
		prefs.setValue(IBugzillaConstants.RESOLUTION_VALUES,
			queryOptionsToString(parser.getResolutionValues()));
		monitor.worked(1);

		// get the new values for the severity field and increment the status monitor
		prefs.setValue(IBugzillaConstants.SEVERITY_VALUES,
			queryOptionsToString(parser.getSeverityValues()));
		monitor.worked(1);

		// get the new values for the priority field and increment the status monitor
		prefs.setValue(IBugzillaConstants.PRIORITY_VALUES,
			queryOptionsToString(parser.getPriorityValues()));
		monitor.worked(1);

		// get the new values for the hardware field and increment the status monitor
		prefs.setValue(IBugzillaConstants.HARDWARE_VALUES,
			queryOptionsToString(parser.getHardwareValues()));
		monitor.worked(1);

		// get the new values for the OS field and increment the status monitor
		prefs.setValue(IBugzillaConstants.OS_VALUES,
			queryOptionsToString(parser.getOSValues()));
		monitor.worked(1);

		// get the new values for the product field and increment the status monitor
		prefs.setValue(IBugzillaConstants.PRODUCT_VALUES,
			queryOptionsToString(parser.getProductValues()));
		monitor.worked(1);

		// get the new values for the component field and increment the status monitor
		prefs.setValue(IBugzillaConstants.COMPONENT_VALUES,
			queryOptionsToString(parser.getComponentValues()));
		monitor.worked(1);

		// get the new values for the version field and increment the status monitor
		prefs.setValue(IBugzillaConstants.VERSION_VALUES,
			queryOptionsToString(parser.getVersionValues()));
		monitor.worked(1);

		// get the new values for the target field and increment the status monitor
		prefs.setValue(IBugzillaConstants.TARGET_VALUES,
			queryOptionsToString(parser.getTargetValues()));
		monitor.worked(1);
	}

	/**
	 * Set the default query options for the bugzilla search
	 */
	private static void setDefaultQueryOptions() {
		// get the preferences store for the bugzilla preferences
		IPreferenceStore prefs = BugzillaPlugin.getDefault().getPreferenceStore();

		// get the default status values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.STATUS_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_STATUS_VALUES));

		// get the default preselected status values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.PRESELECTED_STATUS_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_PRESELECTED_STATUS_VALUES));

		// get the default resolution values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.RESOLUTION_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_RESOLUTION_VALUES));

		// get the default severity values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.SEVERITY_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_SEVERITY_VALUES));

		// get the default priority values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.PRIORITY_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_PRIORITY_VALUES));

		// get the default hardware values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.HARDWARE_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_HARDWARE_VALUES));

		// get the default os values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.OS_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_OS_VALUES));

		// get the default product values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.PRODUCT_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_PRODUCT_VALUES));

		// get the default component values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.COMPONENT_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_COMPONENT_VALUES));

		// get the default version values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.VERSION_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_VERSION_VALUES));

		// get the default target values from the store and set them as the default options
		prefs.setDefault(IBugzillaConstants.TARGET_VALUES,
			queryOptionsToString(IBugzillaConstants.DEFAULT_TARGET_VALUES));
	}

	/**
	 * Turn the array of query options into a string separated by a '!'
	 * 
	 * @param array A string array of query values to be turned into a string
	 * @return The string containing the query options in the array
	 */
	private static String queryOptionsToString(String[] array) {
		// make a new string buffer and go through each element in the array
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			// append the new value to the end and add a '!' as a delimiter
			buffer.append(array[i]);
			buffer.append("!");
		}

		// return the buffer converted to a string
		return buffer.toString();
	}

	/**
	 * Take a string of query options and convert it to an array
	 * 
	 * @param values A string of query options delimited by a '!'
	 * @return A string array containing the query values
	 */
	public static String[] queryOptionsToArray(String values) {
		// create a new string buffer and array list
		StringBuffer buffer = new StringBuffer();
		List<String> options = new ArrayList<String>();

		// make the string into a character array
		char[] chars = values.toCharArray();

		// go through each of the characters in the character array
		for (int i = 0; i < chars.length; i++) {
			if (chars[i] == '!') {
				// if the character is the delimiting value add the buffer to the list
				// and reinitialize it
				options.add(buffer.toString());
				buffer = new StringBuffer();
			} else {
				// if it is a regular character, just add it to the string buffer
				buffer.append(chars[i]);
			}
		}

		// create a new string array with the same size as the array list
		String[] array = new String[options.size()];

		// put each element from the list into the array
		for (int j = 0; j < options.size(); j++)
			array[j] = options.get(j);
		return array;
	}

	/**
	 * Get the password and user name from the keyring
	 *
	 */
    @SuppressWarnings("unchecked")
	private static void getCachedData() {
		// get the map containing the password and username
		Map<String, String> map = Platform.getAuthorizationInfo(FAKE_URL, "Bugzilla", AUTH_SCHEME);

		// get the information from the map and save it
		if (map != null) {
			String username = map.get(INFO_USERNAME);

			if (username != null)
				user = username;
			else
				user = new String("");

			String pwd = map.get(INFO_PASSWORD);

			if (pwd != null)
				password = pwd;
			else
				password = new String("");

			return;
		}

		// if the map was null, set the username and password to be null
		user = new String("");
		password = new String("");
	}

	/**
	 * Gets the bugzilla user name from the preferences
	 * 
	 * @return The string containing the user name
	 */
	public static String getUserName() {
		getCachedData();
		return user;
	}
    
    /**
     * Get whether we are dealing with Bugzilla 2.18 or not
     * @return true if it is 218
     */
    public static boolean is218(){
        return BugzillaPlugin.getDefault().getPreferenceStore().getBoolean(IBugzillaConstants.IS_218);
    }

	/**
	 * Gets the bugzilla password from the preferences
	 * 
	 * @return The string containing the password
	 */
	public static String getPassword() {
		getCachedData();
		return password;
	}

	/**
	 * store the password and username in the keyring
	 * 
	 * @param username The user name to store
	 * @param storePassword The password to store
	 * @param createIfAbsent Whether to create the map if it doesn't exist or not
	 */
    @SuppressWarnings("unchecked")
	private static void storeCache(String username, String storePassword,
			boolean createIfAbsent) {
		// put the password into the Platform map
		Map<String, String> map = Platform.getAuthorizationInfo(FAKE_URL, "Bugzilla", AUTH_SCHEME);

		// if the map doesn't exist, see if we can create a new one
		if (map == null) {
			if (!createIfAbsent)
				return;
			map = new java.util.HashMap<String, String>(10);
		}

		// add the username and password to the map
		if (username != null)
			map.put(INFO_USERNAME, username);
		if (storePassword != null)
			map.put(INFO_PASSWORD, storePassword);

		try {
			// write the map to the keyring 
			Platform.addAuthorizationInfo(FAKE_URL, "Bugzilla", AUTH_SCHEME, map);
		} catch (CoreException e) {
			BugzillaPlugin.log(e.getStatus());
		}
	}

	private static String user;

	private static String password;

	public static final String INFO_PASSWORD = "org.eclipse.team.cvs.core.password"; //$NON-NLS-1$ 

	public static final String INFO_USERNAME = "org.eclipse.team.cvs.core.username"; //$NON-NLS-1$ 

	public static final String AUTH_SCHEME = "";

	public static final URL FAKE_URL;

	static {
		URL temp = null;
		try {
			temp = new URL("http://" + IBugzillaConstants.PLUGIN_ID);
		} catch (MalformedURLException e) {
			BugzillaPlugin.log(new Status(IStatus.WARNING, IBugzillaConstants.PLUGIN_ID,IStatus.OK,"Bad temp server url: BugzillaPreferences", e));
		}
		FAKE_URL = temp;
	}

}

Back to the top