001: /*
002: * Created on Jun 20, 2004
003: *
004: * This file is part of Thingamablog. ( http://thingamablog.sf.net )
005: *
006: * Copyright (c) 2004, Bob Tantlinger All Rights Reserved.
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
021: * USA.
022: */
023: package net.sf.thingamablog;
024:
025: import java.awt.Font;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileNotFoundException;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.net.Authenticator;
032: import java.net.PasswordAuthentication;
033: import java.util.ArrayList;
034: import java.util.List;
035: import java.util.Properties;
036:
037: import javax.swing.UIManager;
038:
039: import net.sf.thingamablog.blog.DiskTemplatePack;
040: import net.sf.thingamablog.blog.ZipTemplatePack;
041:
042: import com.jgoodies.plaf.Options;
043:
044: /**
045: * Static fields and methods for setting/getting Thingamablog's
046: * various application-wide settings and properties
047: *
048: * @author Bob Tantlinger
049: *
050: */
051: public class TBGlobals {
052: /** The name of the application */
053: public static final String APP_NAME = "@APP_NAME@";
054: /** The version of the application */
055: public static final String VERSION = "@VERSION@";
056: /** The build of the application */
057: public static final String BUILD = "@BUILD@";
058: /** The home page of the application */
059: public static final String APP_URL = "@APP_URL@";
060:
061: /** Platform specific path separator */
062: public static final String SEP = System
063: .getProperty("file.separator");
064: /** User's home directory */
065: public static final String USER_HOME = System
066: .getProperty("user.home");
067: /** Directory where app properties are stored */
068: public static final String PROP_DIR = USER_HOME + SEP + ".thinga";
069: /** The app's property file */
070: public static final String PROP_FILE = PROP_DIR + SEP
071: + "thingamablog.properties";
072: /** The application's install dir */
073: public static final String USER_DIR = System
074: .getProperty("user.dir");
075: /** location of default templates */
076: public static final String DEFAULT_TMPL_DIR = USER_DIR + SEP
077: + "template_sets";
078: /** location of spell checker dictionary files */
079: public static final String DICT_DIR = USER_DIR + SEP
080: + "dictionaries";
081:
082: /** File name for the xml file that describes a 'database' */
083: public static final String USER_XML_FILENAME = "user.xml";
084:
085: /** File extensions for common text files */
086: public static final String TEXT_FILE_EXTS[] = { ".html", ".htm",
087: ".shtml", ".xml", ".opml", ".rss", ".rdf", ".css", ".php",
088: ".pl", ".txt", ".py", ".cgi", ".js", ".java", ".c", ".cpp" };
089:
090: /** location of the feed image cache */
091: public static final String IMG_CACHE_DIR = PROP_DIR + SEP
092: + "img_cache";
093:
094: /** 3 column layout constant */
095: public static final int THREE_COL = -5;
096: /** 2 column layout constant */
097: public static final int TWO_COL = -6;
098:
099: private static int layoutStyle = TWO_COL;
100:
101: private static String lastOpenedDatabase = null;
102: private static boolean isStartWithLastDatabase = true;
103: private static boolean startWithSplash = true;
104:
105: private static Font editorFont = new Font(
106: "Monospaced", Font.PLAIN, 12); //$NON-NLS-1$
107: //private static File dictFile = new File(DICT_DIR + SEP + "english.dico"); //$NON-NLS-1$
108:
109: private static String dictionary = "en_US";
110:
111: private static Properties props = new Properties();
112:
113: //proxy stuff
114: private static boolean useSocksProxy;
115: private static boolean isSocksAuth;
116: private static String socksHost = "";
117: private static String socksPort = "1080";
118: private static String socksUser = "";
119: private static String socksPass = "";
120:
121: //auto feed updater stuff
122: private static int feedUpdateInterval = 1800000;//30 minutes
123: private static boolean isAutoFeedUpdate;
124:
125: private static boolean isPingAfterPub = true;
126:
127: //default is the plastic system look and feel
128: private static String lafName = Options
129: .getCrossPlatformLookAndFeelClassName();
130: static {
131: UIManager.installLookAndFeel("Plastic", lafName);
132:
133: //use system laf for default on Win and Mac
134: if (System.getProperty("os.name").toLowerCase().startsWith(
135: "windows")
136: || System.getProperty("os.name").toLowerCase()
137: .startsWith("mac os x"))
138: lafName = UIManager.getSystemLookAndFeelClassName();
139: }
140:
141: /**
142: * Indicate whether a file is a known text file
143: * @param f a file
144: * @return true if the file is a text file, false otherwise
145: */
146: public static boolean isTextFile(File f) {
147: String n = f.getName().toLowerCase();
148: for (int i = 0; i < TEXT_FILE_EXTS.length; i++) {
149: if (n.endsWith(TEXT_FILE_EXTS[i]))
150: return true;
151: }
152:
153: return false;
154: }
155:
156: /**
157: * Indicate whether a file is a known image file
158: * @param f a file
159: * @return true if the file is an image file, false otherwise
160: */
161: public static boolean isImageFile(File f) {
162: String n = f.getName().toLowerCase();
163: return (n.endsWith(".gif") || n.endsWith(".jpg")
164: || n.endsWith(".jpeg") || n.endsWith(".png"));
165: }
166:
167: /**
168: * Get a Thingamablog property
169: * @param key property key
170: * @return the property
171: */
172: public static String getProperty(String key) {
173: return props.getProperty(key);
174: }
175:
176: /**
177: * Set a Thingamablog property
178: * @param key property key
179: * @param val the property
180: */
181: public static void putProperty(String key, Object val) {
182: props.put(key, val);
183: }
184:
185: /**
186: * Initialize the proxy. This should only be called once
187: * during the application's startup
188: */
189: private static void initProxy() {
190: if (useSocksProxy) {
191: System.getProperties().put("socksProxyHost", socksHost);
192: System.getProperties().put("socksProxyPort", socksPort);
193:
194: if (isSocksAuth) {
195: Authenticator auth = new Authenticator() {
196: protected PasswordAuthentication getPasswordAuthentication() {
197: return new PasswordAuthentication(socksUser,
198: socksPass.toCharArray());
199: }
200: };
201: Authenticator.setDefault(auth);
202: }
203: }
204: }
205:
206: /**
207: * Load the properties from the PROP_FILE
208: */
209: public static void loadProperties() {
210: try {
211: FileInputStream fis = new FileInputStream(PROP_FILE);
212: props.load(fis);
213: //Browser.load(props);
214: fis.close();
215:
216: if (props.get("SOCKS_PROXY_HOST") != null)
217: socksHost = props.getProperty("SOCKS_PROXY_HOST");
218: if (props.get("SOCKS_PROXY_PORT") != null)
219: socksPort = props.getProperty("SOCKS_PROXY_PORT");
220: if (props.get("SOCKS_PROXY_USER") != null)
221: socksUser = props.getProperty("SOCKS_PROXY_USER");
222: if (props.get("SOCKS_PROXY_PASSWORD") != null) {
223: socksPass = props.getProperty("SOCKS_PROXY_PASSWORD");
224: socksPass = PasswordUtil.decrypt(socksPass,
225: PasswordUtil.KEY);
226: }
227: //vb1 didn't encrypt the password, so we have to load it
228: //in case the user has upgraded to the current version
229: if (props.get("SOCKS_PROXY_PASS") != null) {
230: socksPass = props.getProperty("SOCKS_PROXY_PASS");
231: props.remove("SOCKS_PROXY_PASS");
232: }
233:
234: String use = props.getProperty("USE_SOCKS_PROXY");
235: useSocksProxy = use != null && use.equals("true");
236: String auth = props.getProperty("SOCKS_PROXY_LOGIN");
237: isSocksAuth = auth != null && auth.equals("true");
238: String update = props.getProperty("AUTO_UPDATE");
239: isAutoFeedUpdate = update != null && update.equals("true");
240: String pap = props.getProperty("PING_AFTER_PUB");
241: //for user that are upgrading
242: if (pap == null)
243: isPingAfterPub = true;
244: else
245: isPingAfterPub = pap.equals("true");
246:
247: initProxy();
248:
249: try {
250: layoutStyle = Integer.parseInt(props
251: .getProperty("LAYOUT"));
252: String name = props.getProperty("EDITOR_FONT_NAME");
253: int size = Integer.parseInt(props
254: .getProperty("EDITOR_FONT_SIZE"));
255: setEditorFont(new Font(name, Font.PLAIN, size));
256: setFeedUpdateInterval(Integer.parseInt(props
257: .getProperty("FEED_UPDATE_INTERVAL")));
258: } catch (Exception ex) {
259: }
260:
261: lastOpenedDatabase = props.getProperty("LAST_DB");
262: if (props.get("START_LAST_DB") != null)
263: isStartWithLastDatabase = props.getProperty(
264: "START_LAST_DB").equals("true");
265: //if(props.get("DICT_FILE") != null)
266: // dictFile = new File(props.getProperty("DICT_FILE"));
267: if (props.get("DICT") != null)
268: dictionary = props.getProperty("DICT");
269: if (props.get("SPLASH_SCREEN") != null)
270: startWithSplash = props.getProperty("SPLASH_SCREEN")
271: .equals("true");
272: if (props.get("LAF") != null)
273: lafName = props.getProperty("LAF");
274:
275: } catch (FileNotFoundException fnfe) {
276: //Browser.init();//use defaults for browser
277: } catch (IOException ex) {
278: ex.printStackTrace();
279: }
280: }
281:
282: /**
283: * Save the application properties to the PROP_FILE
284: */
285: public static void saveProperties() {
286: try {
287: File f = new File(PROP_DIR);
288: f.mkdir();
289: FileOutputStream fos = new FileOutputStream(PROP_FILE);
290: props.put("USE_SOCKS_PROXY", useSocksProxy + "");
291: props.put("SOCKS_PROXY_HOST", socksHost);
292: props.put("SOCKS_PROXY_PORT", socksPort);
293: props.put("SOCKS_PROXY_LOGIN", isSocksAuth + "");
294: props.put("SOCKS_PROXY_USER", socksUser);
295: props.put("SOCKS_PROXY_PASSWORD", PasswordUtil.encrypt(
296: socksPass, PasswordUtil.KEY));
297:
298: props.put("LAYOUT", layoutStyle + "");
299: props.put("LAF", lafName);
300:
301: props.put("EDITOR_FONT_NAME", editorFont.getFamily());
302: props.put("EDITOR_FONT_SIZE", editorFont.getSize() + "");
303: //props.put("DICT_FILE", dictFile.getAbsolutePath());
304: props.put("DICT", dictionary);
305:
306: props.put("SPLASH_SCREEN", startWithSplash + "");
307:
308: if (lastOpenedDatabase != null) {
309: props.put("LAST_DB", lastOpenedDatabase);
310: props
311: .put("START_LAST_DB", isStartWithLastDatabase
312: + "");
313: }
314:
315: props.put("FEED_UPDATE_INTERVAL", feedUpdateInterval + "");
316: props.put("AUTO_UPDATE", isAutoFeedUpdate + "");
317: props.put("PING_AFTER_PUB", isPingAfterPub + "");
318:
319: //Browser.save(props);
320: props.store(fos, "Thingamablog Properties");
321: fos.close();
322:
323: } catch (Exception ex) {
324: ex.printStackTrace();
325: }
326: }
327:
328: /**
329: * Sets the layout style property
330: * @param s The layout style. Either TWO_COL or THREE_COL
331: */
332: public static void setLayoutStyle(int s) {
333: if (s == TWO_COL)
334: layoutStyle = s;
335: else
336: layoutStyle = THREE_COL;
337: }
338:
339: /**
340: * Gets the layout style property
341: * @return The layout style constant - TWO_COL or THREE_COL
342: */
343: public static int getLayoutStyle() {
344: return layoutStyle;
345: }
346:
347: /**
348: * Sets the font used by editors
349: * @param f the editor font
350: */
351: public static void setEditorFont(Font f) {
352: editorFont = f;
353: }
354:
355: /**
356: * Gets the font used by editors
357: * @return a Font
358: */
359: public static Font getEditorFont() {
360: return editorFont;
361: }
362:
363: /**
364: * Sets the dictionary used by editors
365: * @param f a dictionary file
366: */
367: public static void setDictionary(String d) {
368: dictionary = d;
369: }
370:
371: /**
372: * Gets the dictionary used by editors
373: * @return
374: */
375: public static String getDictionary() {
376: return dictionary;
377: }
378:
379: /*
380: public static void setDictionaryFile(File f)
381: {
382: dictFile = f;
383: }
384:
385:
386: public static File getDictionaryFile()
387: {
388: return dictFile;
389: }
390: */
391:
392: //proxy settings
393: /**
394: * set whether or not to used the SOCKS proxy
395: */
396: public static void setUseSocksProxy(boolean b) {
397: useSocksProxy = b;
398: }
399:
400: /**
401: * Indicates whether or not the app uses the SOCKS proxy
402: * @return true if the proxy is enabled, false otherwise
403: */
404: public static boolean isUseSocksProxy() {
405: return useSocksProxy;
406: }
407:
408: /**
409: * Sets the host of the proxy
410: * @param host
411: */
412: public static void setSocksProxyHost(String host) {
413: socksHost = host;
414: }
415:
416: /**
417: * Gets the host of the proxy
418: * @return
419: */
420: public static String getSocksProxyHost() {
421: return socksHost;
422: }
423:
424: /**
425: * Sets the port of the proxy
426: * @param port
427: */
428: public static void setSocksProxyPort(String port) {
429: if (port == null || port.equals(""))
430: socksPort = "1080";
431: else
432: socksPort = port;
433: }
434:
435: /**
436: * Gets the port of the proxy
437: * @return
438: */
439: public static String getSocksProxyPort() {
440: return socksPort;
441: }
442:
443: /**
444: * Indicates whether or not to log-in to the proxy
445: * @param b
446: */
447: public static void setSocksProxyRequiresLogin(boolean b) {
448: isSocksAuth = b;
449: }
450:
451: /**
452: * Indicates whether or not the proxy requires a login
453: * @return
454: */
455: public static boolean isSocksProxyRequiresLogin() {
456: return isSocksAuth;
457: }
458:
459: /**
460: * Sets the user name for the proxy
461: * @param user
462: */
463: public static void setSocksProxyUser(String user) {
464: socksUser = user;
465: }
466:
467: /**
468: * Gets the user name for the proxy
469: * @return
470: */
471: public static String getSocksProxyUser() {
472: return socksUser;
473: }
474:
475: /**
476: * Sets the password for the proxy
477: * @param pw
478: */
479: public static void setSocksProxyPassword(String pw) {
480: socksPass = pw;
481: }
482:
483: /**
484: * Gets the password for the proxy
485: * @return
486: */
487: public static String getSocksProxyPassword() {
488: return socksPass;
489: }
490:
491: /**
492: * Indicates whether a splash screen is displayed at startup
493: * @return
494: */
495: public static boolean isStartWithSplash() {
496: return startWithSplash;
497: }
498:
499: /**
500: * @param b
501: */
502: public static void setStartWithSplash(boolean b) {
503: startWithSplash = b;
504: }
505:
506: /**
507: * Get the look and feel class name of the app
508: * @return
509: */
510: public static String getLookAndFeelClassName() {
511: return lafName;
512: }
513:
514: /**
515: * Sets the class name of the look and feel that should be used
516: * @param name
517: */
518: public static void setLookAndFeelClassName(String name) {
519: lafName = name;
520: }
521:
522: /**
523: * Gets the path to the previously opened USER_XML file
524: * @return
525: */
526: public static String getLastOpenedDatabase() {
527: return lastOpenedDatabase;
528: }
529:
530: /**
531: * @param string
532: */
533: public static void setLastOpenedDatabase(String string) {
534: lastOpenedDatabase = string;
535: }
536:
537: /**
538: * @return
539: */
540: public static boolean isStartWithLastDatabase() {
541: return isStartWithLastDatabase;
542: }
543:
544: /**
545: * @param b
546: */
547: public static void setStartWithLastDatabase(boolean b) {
548: isStartWithLastDatabase = b;
549: }
550:
551: /**
552: * @return Returns the feedUpdateInterval.
553: */
554: public static int getFeedUpdateInterval() {
555: return feedUpdateInterval;
556: }
557:
558: /**
559: * @param feedUpdateInterval The feedUpdateInterval to set.
560: */
561: public static void setFeedUpdateInterval(int ms) {
562: feedUpdateInterval = ms;
563: }
564:
565: /**
566: * @return Returns the isAutoFeedUpdate.
567: */
568: public static boolean isAutoFeedUpdate() {
569: return isAutoFeedUpdate;
570: }
571:
572: /**
573: * @param isAutoFeedUpdate The isAutoFeedUpdate to set.
574: */
575: public static void setAutoFeedUpdate(boolean b) {
576: isAutoFeedUpdate = b;
577: }
578:
579: public static boolean isPingAfterPublish() {
580: return isPingAfterPub;
581: }
582:
583: public static void setPingAfterPublish(boolean b) {
584: isPingAfterPub = b;
585: }
586:
587: public static File getPreviewDirectory() {
588: return new File(PROP_DIR, "preview");
589: }
590:
591: public static File getUserInstalledTemplatesDirectory() {
592: return new File(PROP_DIR, "installed_templates");
593: }
594:
595: public static File getDefaultTemplateDirectory() {
596: return new File(DEFAULT_TMPL_DIR);
597: }
598:
599: public static List getAllAvailableTemplates() {
600: List packs = getDefaultTemplates();
601: packs.addAll(getUserInstalledTemplates());
602: return packs;
603: }
604:
605: public static List getDefaultTemplates() {
606: File dir = getDefaultTemplateDirectory();
607: if (!dir.isDirectory()) {
608: dir.mkdirs();
609: return new ArrayList();
610: }
611:
612: return getTemplatePacks(dir.listFiles());
613: }
614:
615: public static List getUserInstalledTemplates() {
616: File dir = getUserInstalledTemplatesDirectory();
617: if (!dir.isDirectory()) {
618: dir.mkdirs();
619: return new ArrayList();
620: }
621:
622: return getTemplatePacks(dir.listFiles());
623: }
624:
625: private static List getTemplatePacks(File[] files) {
626: List packs = new ArrayList();
627: for (int i = 0; i < files.length; i++) {
628: if (files[i].isDirectory()) {
629: try {
630: packs.add(new DiskTemplatePack(files[i]));
631: } catch (Exception ex) {
632: ex.printStackTrace();
633: }
634: } else if (files[i].isFile()
635: && files[i].getName().toLowerCase()
636: .endsWith(".zip")) {
637: try {
638: packs.add(new ZipTemplatePack(files[i]));
639: } catch (Exception ex) {
640: ex.printStackTrace();
641: }
642: }
643:
644: }
645:
646: return packs;
647: }
648: }
|