KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > WhisperIM


1 import org.jivesoftware.smack.*;
2 import org.jivesoftware.smackx.ServiceDiscoveryManager;
3
4 import whisper.*;
5
6 import java.awt.*;
7 import java.awt.event.KeyEvent JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.PrintStream JavaDoc;
12 import java.util.ListResourceBundle JavaDoc;
13 import java.util.ResourceBundle JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.security.SecureRandom JavaDoc;
16 import javax.swing.*;
17 import javax.swing.plaf.*;
18 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
19
20 /** The main class that starts Whisper IM and holds application wide variables.*/
21 public final class WhisperIM{
22     
23     private static final String JavaDoc ERROR_FILE_NAME="error.txt"; // where we dump error info for criticl errors
24
/** The version as a string, eg 1.0*/
25     public static final String JavaDoc VERSION="0.2";
26     /** Version info eg Non Commercial Use.*/
27     public static final String JavaDoc VERSION_INFO="Free For Non-Commercial Use";
28     /** Name of user preferences file.
29     * Is prefs.properties.*/

30     public static final String JavaDoc USER_PREF_FILE_NAME="prefs.properties";
31     /** Name of the Key Cache Key filename.
32     * kck.dat */

33     public static final String JavaDoc KCK_FILE_NAME="kck.dat";
34     /** The configuration file name - "config.dat".*/
35     public static final String JavaDoc CONFIG_FILE_NAME="config.dat";
36     /** The keypair file name - "keypair.dat".*/
37     public static final String JavaDoc KEYPAIR_FILENAME="keypair.dat";
38     /** The configuration file.*/
39     public static File JavaDoc CONFIG_FILE;
40     /** Config properties.*/
41     public static Properties JavaDoc Config;
42     /** User preferences properties.*/
43     public static Properties JavaDoc UserPref;
44     /** Langauge resource bundle - initialy set using default locale.*/
45     public static ResourceBundle JavaDoc lang;
46     /** Flag indicating if we are running on MacOs.*/
47     public static boolean isMac=false;
48     /** Flag indicating if user prefs have been loaded.*/
49     public static boolean userPrefLoaded=false;
50     /** Current look and feel - native or metal.*/
51     public static String JavaDoc currentLaf="metal";
52     /** Flag indicating if we on multiuser shared with no login machine (eg win98).*/
53     public static boolean sharedMachine=false;
54     /** Directory where account(s) is stored.*/
55     public static File JavaDoc accountDir;
56     /** Directory where user files are stored - sub dir of accountDir.*/
57     public static File JavaDoc userDir;
58     /** The whisper key pair .*/
59     public static KeyPair KEYPAIR;
60     /** Acount settings.*/
61     public static AccountSettings accountSettings;
62     /** Secure random source.*/
63     public static SecureRandom JavaDoc SR;
64     /** The application wide help window.*/
65     public static HelpWindow Help_Window;
66     /** The log in window.*/
67     public static LogInFrame Login_Window;
68     /** The configuration window.*/
69     public static ConfigDialog Config_Window;
70     /** New account window.*/
71     public static NewAccountWindow New_Account_Window;
72     /** The main window.*/
73     public static JMainFrame MainWindow;
74     
75     public static JAboutDialog About;
76     /** Key code for the help key.
77     * F1 on pc's, HELP on macs.*/

78     public static int HELP_KEY=KeyEvent.VK_F1;
79     
80     public static JSplash splash;
81     
82     public static void main(String JavaDoc[] args){
83         try{// Top level exception handler
84
// Re-assign stderr to error file
85
System.setErr(new PrintStream JavaDoc(new FileOutputStream JavaDoc(ERROR_FILE_NAME,true),true));
86             // Show splash screen
87
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
88                 public void run(){
89                     try{
90                         splash=new JSplash();
91                         splash.show();
92                         splash.inc();
93                     }
94                     catch(Throwable JavaDoc t){
95                         GUI.errorStop(null,t);
96                     }
97                 }
98             });
99             
100             lang=ListResourceBundle.getBundle("Lang");
101             CONFIG_FILE=new File JavaDoc(CONFIG_FILE_NAME);
102             SR=new SecureRandom JavaDoc();
103             accountSettings=new AccountSettings();
104             Config=new Properties JavaDoc(new DefaultConfig());
105             UserPref=new Properties JavaDoc(new DefaultUserPref());
106             
107             // Load config file
108
try{
109                 Config.load(new FileInputStream JavaDoc(CONFIG_FILE));
110             }
111             catch(Exception JavaDoc e){
112                 GUI.errorStop(lang.getString("cannot load config file"),e);
113             }
114             
115             javax.swing.SwingUtilities.invokeLater(new SplashInc());
116             
117             // configure smack
118
SmackConfiguration.setKeepAliveInterval(Integer.parseInt(Config.getProperty("keepalive")));
119             SmackConfiguration.setPacketReplyTimeout(Integer.parseInt(Config.getProperty("timeout")));
120             VCardIQProvider.Install(); // install vcard support
121
SearchIQProvider.install(); //Install search iq packet support
122
VersionIQ.install();//Install jabber:iq:version support
123
DelayExtensionProvider.install(); //Install jabber:x:delay support
124
Chat.setFilteredOnThreadID(false);
125             Roster.setDefaultSubscriptionMode(Roster.SUBSCRIPTION_MANUAL);
126             XMPPConnection.DEBUG_ENABLED=false;
127             javax.swing.SwingUtilities.invokeLater(new SplashInc());
128             
129             // See if we on a mac
130
if(System.getProperty("os.name").toLowerCase().indexOf("mac")!=-1){
131                 isMac=true;
132                 // set the help key
133
HELP_KEY=KeyEvent.VK_HELP;
134             }
135             
136             setAccountDir();
137             
138             javax.swing.SwingUtilities.invokeLater(new SplashInc());
139             
140             // Set the UI look and feel. We use metal unless user prefs specify not to
141
// or user prefs not loaded yet and we on a mac
142
ToolTipManager.sharedInstance().setEnabled(true); //enable tool tips
143
if(!(UserPref.getProperty("laf").compareTo("native")==0 || ((!userPrefLoaded) && isMac))){
144                 //UIManager.put("ClassLoader", UIManager.class.getClassLoader());
145
UIManager.setLookAndFeel(new com.incors.plaf.kunststoff.KunststoffLookAndFeel());
146             }
147             else{ //set native
148
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
149                 currentLaf="native";
150             }
151             
152             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
153                 public void run(){
154                     try{
155                     splash.inc();
156                     //recommend window decorations
157
JFrame.setDefaultLookAndFeelDecorated(true);
158                     JDialog.setDefaultLookAndFeelDecorated(true);
159                     splash.inc();
160                     About=new JAboutDialog();
161                     splash.inc();
162                     MainWindow=new JMainFrame();
163                     splash.inc();
164                     Login_Window=new LogInFrame();
165                     splash.inc();
166                     Login_Window.show();
167                     splash.dispose();
168                     splash=null;
169                     }
170                     catch(Throwable JavaDoc t){
171                         GUI.errorStop(null,t);
172                     }
173                 }
174             });
175         } // end of top level try block
176
catch(Throwable JavaDoc t){ //top level catch
177
t.printStackTrace();
178             GUI.errorStop(null,t);
179         }
180     }
181     /* Set the accountDir.*/
182     public static void setAccountDir() throws java.io.IOException JavaDoc{
183             /* Set the accountDir.
184             We also try to set userDir and Load user prefernces if we can.
185             On multi-login and network environments, we can now if the account already exists.
186             If no account exists yet, or we are in a single machine shared login environment (EG win98),
187             we can't load user prefs until account created or selected. */

188             accountDir=new File JavaDoc("accounts");//dir for accounts in shared environ.
189
sharedMachine=false;
190             if(Config.getProperty("accountLocation").compareTo("application")!=0){
191                 // userhome or other network location
192
if(Config.getProperty("accountLocation").compareTo("userhome")==0){ //userhome location
193
accountDir=new File JavaDoc(System.getProperties().getProperty("user.home"));
194                 }
195                 else{ //other network location
196
String JavaDoc dir=Config.getProperty("accountLocation");
197                     accountDir=new File JavaDoc(dir.replaceAll("USER",System.getProperties().getProperty("user.name")));
198                 }
199                 // check account dir exists
200
if(!accountDir.exists()){
201                     GUI.showWarning(null,null,"addne",accountDir.getCanonicalPath());
202                 }
203                 // set userDir
204
userDir=new File JavaDoc(accountDir,"whisper");
205                 // If user dir exists (ie account already exists) , load the user prefs
206
if(userDir.exists() && userDir.isDirectory()){
207                     GUI.loadPrefs();
208                 }
209             }
210             else{ //application dir
211
sharedMachine=true;
212                 // check accounts directory exists or chuck error
213
if(!accountDir.exists()){
214                     GUI.showWarning(null,null,"addne",accountDir.getCanonicalPath());
215                 }
216             }
217         }
218         
219 }
Popular Tags