1 /**
2  * EGroupware - Notifications Java Desktop App
3  *
4  * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
5  * @package notifications
6  * @subpackage jdesk
7  * @link http://www.egroupware.org
8  * @author Stefan Werfling <stefan.werfling@hw-softwareentwicklung.de>
9  * @author Maik Hüttner <maik.huettner@hw-softwareentwicklung.de>
10  */
11 
12 package egroupwaretray;
13 
14 import edu.stanford.ejalbert.BrowserLauncher;
15 import egroupwaretray.settings.egwSettingUrl;
16 import java.awt.MenuItem;
17 import java.awt.SystemTray;
18 import java.awt.TrayIcon;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Locale;
24 import java.util.Timer;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.swing.JDialog;
28 import javax.swing.JFrame;
29 import javax.swing.JOptionPane;
30 
31 /**
32  * jegwMain
33  *
34  * TODO http://www.java-blog-buch.de/d-plugin-entwicklung-in-java/
35  *
36  * @author Stefan Werfling <stefan.werfling@hw-softwareentwicklung.de>
37  */
38 public class jegwMain implements ActionListener
39 {
40     private hwTrayIcon hwtray   = new hwTrayIcon(
41             jegwConst.getConstTag("egwtitle"),
42             jegwConst.getConstTag("egwiconoff"));
43 
44     private jegwconfig egwconfig  = null;
45     private KeyArray activconf  = null;
46     private jegwhttp egwhttp    = new jegwhttp(this);
47     private KeyArray egwcookie  = null;
48     private Timer egwttask      = new Timer();
49     private Timer egwtcd        = new Timer();
50 
51     private jegwapplications applications = new jegwapplications(this);
52 
53     private hwTrayIconChanger _iconchanger = null;
54 
jegwMain()55     public jegwMain()
56     {
57         try
58         {
59 			if( !SystemTray.isSupported() )
60 			{
61 				jegwMain.infoDialog("The system tray isnt supported!", "Error");
62                 System.exit(0);
63 			}
64 
65 
66             /**
67              * Einstellungen Laden
68              */
69             try
70             {
71                 this.egwconfig = new jegwconfig(
72 					jegwConst.getConstTag("egwaccountfile"));
73 
74                 this.egwconfig.loadConfig();
75             }
76             catch( Exception exp )
77             {
78                 jegwMain.infoDialog(
79 					"Error on load const file: no such file.",
80                     "Warning");
81 
82                 System.exit(0);
83             }
84 
85 			// Debuging
86 			Boolean _debuging = ( jegwConst.getConstTag("egw_debuging").trim()
87 				== "1" ? true : false);
88 
89 			egwDebuging.setDebuging(_debuging);
90 
91 			try
92 			{
93 				egwDebuging.setLevel(Level.parse(
94 					jegwConst.getConstTag("egw_debuging_level").trim()));
95 			}
96 			catch(Exception exp)
97 			{
98 				egwDebuging.setLevel(Level.ALL);
99 			}
100 			// END
101 
102             if( this.egwconfig.getCXMLM().countConf() < 1 )
103             {
104 				String turl = jegwConst.getConstTag("egw_dc_url").trim();
105 				String tlogindomain = jegwConst.getConstTag("egw_dc_logindomain").trim();
106 				String tusername = jegwConst.getConstTag("egw_dc_username").trim();
107 
108 				if( (!turl.isEmpty()) &&
109 					(!tlogindomain.isEmpty()) &&
110 					(!tusername.isEmpty()) )
111 				{
112 					KeyArray config = new KeyArray(jegwxmlconfig.CONF_STRUCT);
113 
114 					config.add("host", "oneconfig");
115 					config.add("user", tusername);
116 					config.add("logindomain", tlogindomain);
117 					config.add("egwurl", turl);
118 
119 					this.egwconfig.getCXMLM().addConf(config);
120 					this.egwconfig.saveConfig();
121 				}
122 				else
123 				{
124 					jegwMain.infoDialog(
125 						jegwConst.getConstTag("egw_msg_config_create"),
126 						jegwConst.getConstTag("info_info")
127 						);
128 
129 					JFrame jf = EgroupwareTrayApp.getApplication().getMainFrame();
130 					JDialog dialog = new egwSettingUrl(jf, true, this.egwconfig);
131 
132 					try {
133 						dialog.setIconImage(hwTrayIcon.getImage(jegwConst.getConstTag("egwicon")));
134 					} catch (IOException ex) {
135 						Logger.getLogger(hwTrayIcon.class.getName()).log(Level.SEVERE, null, ex);
136 					}
137 
138 					dialog.setAlwaysOnTop(true);
139 					dialog.setVisible(true);
140 
141 					if( this.egwconfig.getCXMLM().countConf() < 1 )
142 					{
143 						jegwMain.infoDialog(
144 							jegwConst.getConstTag("egw_msg_setting_aborting"),
145 							jegwConst.getConstTag("info_info"));
146 
147 						System.exit(0);
148 					}
149 				}
150             }
151 			else
152 			{
153 				// Certificates load and set
154 				String sslcert = this.egwconfig.getCXMLM().getActivConf().getString("sslcert");
155 
156 				if( !sslcert.trim().isEmpty() )
157 				{
158 					BaseHttp.getTrustManager().setAcceptedCerts(sslcert);
159 				}
160 			}
161 
162             // Tray Icon erstellen
163             //this.hwtray.AddMenuItem("InfoDialog", this);
164 
165             this.hwtray.addMenuItem(jegwConst.getConstTag("MI_browser"), this);
166             this.hwtray.addMenuItem(jegwConst.getConstTag("MI_login"), this);
167             this.hwtray.addMenuItem(jegwConst.getConstTag("MI_logout"), this);
168             this.hwtray.addMenuItem(jegwConst.getConstTag("MI_settings"), this);
169             this.hwtray.addMenuItem(jegwConst.getConstTag("MI_exit"), this);
170             this.hwtray.addTrayAction(this);
171             this.hwtray.createTrayIcon();
172 
173             this.hwtray.setDisableMenuItem(jegwConst.getConstTag("MI_login"), true);
174             this.hwtray.setDisableMenuItem(jegwConst.getConstTag("MI_logout"), false);
175 
176             this._iconchanger = new hwTrayIconChanger(this.hwtray);
177 
178             this.egwstart();
179         }
180         catch (Exception ex)
181         {
182             Logger.getLogger(jegwMain.class.getName()).log(Level.SEVERE, null, ex);
183             jegwMain.debugDialog(ex.getMessage());
184 			egwDebuging.log.log(Level.SEVERE, null, ex);
185         }
186     }
187 
getSystemLang()188 	private Locale getSystemLang()
189 	{
190 		return Locale.getDefault();
191 	}
192 
egwstart()193     private void egwstart()
194     {
195         this.egwttask      = new Timer();
196         this.egwtcd        = new Timer();
197 
198         // Task erstellen
199         jegwtask task = new jegwtask();
200         task.addActionListener(this);
201 
202         // Automatisches Login
203         this.egwAutologin();
204 
205 		long period = Long.parseLong(jegwConst.getConstTag("egw_dc_timeout_notify"));
206 
207         // Automatische Task starten
208         this.egwttask.schedule(task, 5000, period);
209 
210         /**
211          * Info Time out task
212          */
213         jegwTaskCoundownViewer tviewer = new jegwTaskCoundownViewer();
214         tviewer.addActionListener(this);
215         tviewer.setCounDown(4000);
216 
217         this.egwtcd.schedule(tviewer, 0, 1000);
218     }
219 
egwAutologin()220     private void egwAutologin()
221     {
222         this.activconf = (KeyArray) this.egwconfig.getCXMLM().getActivConf().clone();
223 
224         egwPasswordCrypt egwcp = new egwPasswordCrypt();
225 
226         if( this.activconf.getString("password").length() != 0 )
227         {
228             try
229             {
230                 this.activconf.add("password",
231                         egwcp.decode(this.activconf.getString("password")));
232             }
233             catch(Exception exp)
234             {
235                 this.activconf.add("password", "");
236                 this.exceptionMsg(exp.getMessage(), exp);
237             }
238         }
239 
240         if( this.activconf.getString("password").length() == 0 )
241         {
242             JFrame jf = EgroupwareTrayApp.getApplication().getMainFrame();
243             JDialog dialog = new jegwPasswordDialog(jf, true);
244 
245 			try {
246 				dialog.setIconImage(hwTrayIcon.getImage(jegwConst.getConstTag("egwicon")));
247 			} catch (IOException ex) {
248 				Logger.getLogger(hwTrayIcon.class.getName()).log(Level.SEVERE, null, ex);
249 			}
250 
251 			((jegwPasswordDialog) dialog).setUH(
252 				this.activconf.getString("user"),
253                 this.activconf.getString("host")
254 				);
255 
256 			dialog.setAlwaysOnTop(true);
257             dialog.setVisible(true);
258             String password = ((jegwPasswordDialog) dialog).getPassword();
259 
260             if( password.length() != 0 )
261             {
262                 this.activconf.add("password", password);
263 
264                 // Passwort Speichern
265                 if( ((jegwPasswordDialog) dialog).isSavePassword() )
266                 {
267                     for(int i=0; i<this.egwconfig.getCXMLM().countConf(); i++)
268                     {
269                         KeyArray conf = this.egwconfig.getCXMLM().getConf(i);
270 
271                         if( (conf.getString("user").compareTo(
272                                 this.activconf.getString("user")) == 0) &&
273                                 (conf.getString("host").compareTo(
274                                 this.activconf.getString("host")) == 0) )
275                         {
276                             this.egwconfig.getCXMLM().deleteConf(i);
277 
278                             try
279                             {
280                                 conf.add("password", egwcp.encode(password));
281                             }
282                             catch(Exception exp)
283                             {
284                                 // passwort konnte nicht gespeichert werden
285                                 this.exceptionMsg(exp.getMessage(), exp);
286                             }
287 
288                             this.egwconfig.getCXMLM().addConf(conf);
289 
290                             try
291                             {
292                                 this.egwconfig.saveConfig();
293                             }
294                             catch(Exception exp)
295                             {
296                                 this.exceptionMsg(exp.getMessage(), exp);
297                             }
298                         }
299                     }
300                 }
301             }
302             else
303             {
304                 // Benutzerabbruch kein Passwort
305                 if( jegwMain.confirmDialog(
306 					jegwConst.getConstTag("egw_msg_login_aborting"),
307 					jegwConst.getConstTag("info_info")) == 0 )
308                 {
309                     System.exit(0);
310                 }
311 
312                 return;
313             }
314         }
315 
316 
317         try
318         {
319             this.hwtray.showBallon(jegwConst.getConstTag("info_info"),
320                     jegwConst.getConstTag("egw_msg_login_start"),
321                     TrayIcon.MessageType.INFO);
322 
323             this.egwcookie = this.egwhttp.egwLogin(this.activconf);
324         }
325         catch( Exception ex)
326         {
327             this.exceptionMsg(ex.getMessage(), ex);
328         }
329 
330         if( this.egwcookie != null )
331         {
332             this.hwtray.showBallon(
333 				jegwConst.getConstTag("info_login"),
334 				jegwConst.getConstTag("egw_txt_account") + " " +
335 				this.activconf.getString("user") + " " +
336 				jegwConst.getConstTag("egw_txt_login") + " " +
337 				this.activconf.getString("host") + ".",
338 				TrayIcon.MessageType.INFO);
339 
340             // Icon ändern
341             this.hwtray.changeIcon(jegwConst.getConstTag("egwicon"));
342 
343             this.hwtray.setDisableMenuItem(jegwConst.getConstTag("MI_login"), false);
344             this.hwtray.setDisableMenuItem(jegwConst.getConstTag("MI_logout"), true);
345 
346             // Webdav mount
347             //this.mountEgwWebdav();
348         }
349     }
350 
mountEgwWebdav()351     private void mountEgwWebdav()
352     {
353         KeyArray conf = this.activconf;
354 
355         String apps = (String) conf.get("webdav_apps");
356 
357         if( !apps.equals("") )
358         {
359 
360         }
361     }
362 
debugDialog(String str)363     static public void debugDialog(String str)
364     {
365         JOptionPane.showMessageDialog(
366 			EgroupwareTrayApp.getApplication().getMainFrame(),
367 			str, "Debug", JOptionPane.ERROR_MESSAGE);
368     }
369 
infoDialog(String str, String title)370     static public void infoDialog(String str, String title)
371     {
372         JOptionPane.showMessageDialog(
373 			EgroupwareTrayApp.getApplication().getMainFrame(),
374 			str, title, JOptionPane.INFORMATION_MESSAGE);
375     }
376 
confirmDialog(String str, String title)377     static public int confirmDialog(String str, String title)
378     {
379         return JOptionPane.showConfirmDialog(
380 			EgroupwareTrayApp.getApplication().getMainFrame(),
381 			str, title, JOptionPane.YES_NO_OPTION);
382     }
383 
logout()384     private void logout()
385     {
386         this.egwttask.cancel();
387         this.egwtcd.cancel();
388 
389         this.hwtray.setDisableMenuItem(jegwConst.getConstTag("MI_login"), true);
390         this.hwtray.setDisableMenuItem(jegwConst.getConstTag("MI_logout"), false);
391 
392         this.applications.clearApps();
393 
394         try
395         {
396             this.egwhttp.egwLogout(this.activconf, this.egwcookie);
397         }
398         catch( Exception ex)
399         {
400             this.exceptionMsg(ex.getMessage(), ex);
401         }
402     }
403 
404 	/**
405 	 * openBrowser
406 	 * open Browser with address to Egroupware
407 	 */
openBrowser()408     public void openBrowser()
409     {
410         if( (this.egwcookie != null) && (this.egwhttp.egwIsEGWLogin()) )
411         {
412 			String openlink = this.egwhttp.egwGetOpenEGWLink(
413 				this.activconf,
414 				this.egwcookie,
415 				""
416 				);
417 
418 			try
419 			{
420 				BrowserLauncher launcher = new BrowserLauncher();
421 				launcher.openURLinBrowser(openlink);
422 
423 				this.hwtray.showBallon(
424 					jegwConst.getConstTag("info_info"),
425 					jegwConst.getConstTag("egw_msg_start_browser"),
426 					TrayIcon.MessageType.INFO);
427 			}
428 			catch( Exception exp )
429 			{
430 				// Error
431 				this.hwtray.showBallon(
432 					jegwConst.getConstTag("info_info"),
433 					jegwConst.getConstTag("egw_msg_start_browser_error"),
434 					TrayIcon.MessageType.ERROR);
435 
436 				egwDebuging.log.log(Level.SEVERE, null, exp);
437 			}
438 		}
439     }
440 
441 	/**
442 	 * checkIconChangeInfo
443 	 * check is icon change for information
444 	 *
445 	 */
checkIconChangeInfo()446     private void checkIconChangeInfo()
447     {
448         if( this.applications.getDBufferCount() > 0 )
449         {
450             if( this._iconchanger.getIconCount() == 0 )
451             {
452                 this._iconchanger.addIcon(jegwConst.getConstTag("egwicon"));
453                 this._iconchanger.addIcon(jegwConst.getConstTag("egwiconinfo"));
454             }
455 
456             this._iconchanger.changeIcon();
457         }
458         else
459         {
460             this._iconchanger.clearIconList();
461             this.hwtray.changeIcon(jegwConst.getConstTag("egwicon"));
462         }
463     }
464 
actionPerformed(ActionEvent e)465     public void actionPerformed(ActionEvent e)
466     {
467         String sclass   = e.getSource().getClass().getName();
468         Object cclass   = e.getSource();
469         String cmd      = e.getActionCommand();
470 
471         /**
472          * Action Menu
473          */
474         if( sclass.compareTo("java.awt.MenuItem") == 0 )
475         {
476             MenuItem item = (MenuItem) cclass;
477 
478             /**
479              * Action Exit
480              */
481             if(item.getLabel().compareTo(jegwConst.getConstTag("MI_exit")) == 0)
482             {
483                 if( this.egwhttp.egwIsEGWLogin() )
484                 {
485                     this.logout();
486                 }
487 
488                 System.exit(0);
489             }
490 
491             /**
492              * Action Settings
493              */
494             if( item.getLabel().compareTo(
495 				jegwConst.getConstTag("MI_settings")) == 0 )
496             {
497                 JFrame jf = EgroupwareTrayApp.getApplication().getMainFrame();
498                 JDialog dialog = new egwSettingUrl(jf, true, this.egwconfig);
499 
500                 try {
501 					dialog.setIconImage(hwTrayIcon.getImage(jegwConst.getConstTag("egwicon")));
502 				} catch (IOException ex) {
503 					Logger.getLogger(hwTrayIcon.class.getName()).log(Level.SEVERE, null, ex);
504 					egwDebuging.log.log(Level.SEVERE, null, ex);
505 				}
506 
507 				dialog.setAlwaysOnTop(true);
508                 dialog.setVisible(true);
509                 // Aktuelle Config benutzen
510                 this.egwAutologin();
511             }
512 
513             /**
514              * Action Login
515              */
516             if( item.getLabel().compareTo(jegwConst.getConstTag("MI_login") ) == 0)
517             {
518                 this.egwstart();
519             }
520 
521             /**
522              * Action Logout
523              */
524             if( item.getLabel().compareTo(jegwConst.getConstTag("MI_logout") ) == 0)
525             {
526                 this.logout();
527             }
528 
529             /**
530              * Action Browser Open
531              */
532             if( item.getLabel().compareTo(jegwConst.getConstTag("MI_browser") ) == 0)
533             {
534                 this.openBrowser();
535             }
536 
537             /**
538              * Action Info Dialog
539              */
540             if( item.getLabel().compareTo("InfoDialog") == 0 )
541             {
542                 JFrame jf = EgroupwareTrayApp.getApplication().getMainFrame();
543                 JDialog dialog = new jegwInfoDialog(jf, true);
544                 dialog.setVisible(true);
545             }
546 
547         }
548 
549         if( sclass.compareTo("egroupwaretray.hwTrayIcon") == 0 )
550         {
551             if( (cmd != null) && (cmd.compareTo("clicked") == 0) )
552             {
553                 this.applications.showDialog(this);
554             }
555         }
556 
557         if( sclass.compareTo("java.awt.TrayIcon") == 0 )
558         {
559             TrayIcon ticon = (TrayIcon) cclass;
560 
561             this.openBrowser();
562         }
563 
564         if( sclass.compareTo("egroupwaretray.jegwtask") == 0 )
565         {
566             if( this.egwhttp.egwIsEGWLogin() )
567             {
568                 try
569                 {
570                     /**
571                      * Info Time out task
572                      */
573                     jegwTaskCoundownViewer tviewer = new jegwTaskCoundownViewer();
574                     tviewer.addActionListener(this);
575 
576 					long period = Long.parseLong(jegwConst.getConstTag("egw_dc_timeout_notify"));
577 
578                     tviewer.setCounDown((int)period);
579 
580                     this.egwtcd.schedule(tviewer, 0, 1000);
581 
582                     /**
583                      * Daten laden
584                      */
585                     ArrayList data = this.egwhttp.egwLoadEGWData(this.activconf, this.egwcookie);
586                     //System.out.print(data);
587                     this.applications.setNewApplicationDatas(data, this);
588                 }
589                 catch( Exception ex)
590                 {
591                     this.exceptionMsg(ex.getMessage(), ex);
592                 }
593             }
594             else
595             {
596                 //
597                 if( jegwMain.confirmDialog(
598 					jegwConst.getConstTag("egw_msg_repeat_login"),
599                     jegwConst.getConstTag("egw_txt_title_login")) == 0 )
600                 {
601                     this.egwAutologin();
602                 }
603             }
604         }
605 
606         /**
607          * Zeit anzeige updaten
608          */
609         if( sclass.compareTo("egroupwaretray.jegwTaskCoundownViewer") == 0 )
610         {
611             this.hwtray.setTooltip(jegwConst.getConstTag("egwtitle") + " " +
612 				String.format(jegwConst.getConstTag("egw_txt_update"),
613 				(Object[]) new String[]{e.getActionCommand().toString()})
614 				);
615 
616             this.checkIconChangeInfo();
617         }
618 
619         if( sclass.compareTo("egroupwaretray.jegwInfoDialog") == 0 )
620         {
621             String[] tmp = e.getActionCommand().toString().split(":");
622 
623             if( tmp[0].compareTo("OPENAPP") == 0 )
624             {
625                 if( (this.egwcookie != null) && (this.egwhttp.egwIsEGWLogin()) )
626                 {
627 					String menuaction = "";
628 
629 					KeyArray tmsg = this.applications.searchMsgById(tmp[1]);
630 
631 					if( tmsg != null )
632 					{
633 						this.applications.removeMsgById(tmp[1]);
634 						menuaction = tmsg.getString("link");
635 					}
636 
637 					String openlink = this.egwhttp.egwGetOpenEGWLink(
638 							this.activconf,
639 							this.egwcookie,
640 							menuaction
641 							);
642 
643 					try
644 					{
645 						BrowserLauncher launcher = new BrowserLauncher();
646 						launcher.openURLinBrowser(openlink);
647 
648 						this.hwtray.showBallon(
649 							jegwConst.getConstTag("info_info"),
650 							jegwConst.getConstTag("egw_msg_start_browser"),
651 							TrayIcon.MessageType.INFO);
652 					}
653 					catch(Exception exp)
654 					{
655 						egwDebuging.log.log(Level.SEVERE, null, exp);
656 
657 						// Fehler
658 						this.hwtray.showBallon(
659 							jegwConst.getConstTag("info_info"),
660 							jegwConst.getConstTag("egw_msg_start_browser_error"),
661 							TrayIcon.MessageType.ERROR);
662 					}
663                 }
664             }
665         }
666     }
667 
exceptionMsg(String emsg, Exception ex)668     private void exceptionMsg(String emsg, Exception ex)
669     {
670         String[] msg = emsg.split(":");
671 
672         this.hwtray.changeIcon(jegwConst.getConstTag("egwiconerror"));
673 
674         if( msg[0].compareTo("NETERROR") == 0 )
675         {
676             jegwMain.infoDialog(
677 				jegwConst.getConstTag("egw_msg_connection_error"),
678 				jegwConst.getConstTag("egw_txt_connection_error")
679 				);
680 
681             return;
682         }
683         else if( msg[0].compareTo("LOGIN") == 0 )
684         {
685             jegwMain.infoDialog(
686 				jegwConst.getEGWStatus(msg[1]),
687 				jegwConst.getConstTag("egw_txt_title_login")
688 				);
689 
690             if( jegwMain.confirmDialog(
691 					jegwConst.getConstTag("egw_msg_repeat_login"),
692                     jegwConst.getConstTag("egw_txt_title_login")) == 0 )
693 			{
694                 this.egwAutologin();
695             }
696 
697             return;
698         }
699         else if( msg[0].compareTo("PERMISSIONDENIED") == 0 )
700         {
701             jegwMain.infoDialog(
702 				jegwConst.getConstTag("egw_msg_premission_denied") +
703 				"\r\n" +
704 				jegwConst.getConstTag("egw_msg_contact_admin"),
705 				jegwConst.getConstTag("egw_txt_title_premission"));
706 
707             return;
708         }
709         else if( msg[0].compareTo("PAGENOTFOUND") == 0 )
710         {
711              jegwMain.infoDialog(
712 				jegwConst.getConstTag("egw_msg_page_not_found"),
713 				jegwConst.getConstTag("egw_txt_title_notifier")
714 				);
715 
716              return;
717         }
718         else if( msg[0].compareTo("HOSTNOTFOUND") == 0 )
719         {
720             jegwMain.infoDialog(
721 				jegwConst.getConstTag("egw_msg_domain_not_found"),
722 				jegwConst.getConstTag("egw_txt_title_notifier")
723 				);
724 
725             return;
726         }
727         else
728         {
729             jegwMain.debugDialog(msg[0]);
730         }
731 
732         Logger.getLogger(jegwMain.class.getName()).log(Level.SEVERE, null, ex);
733 		egwDebuging.log.log(Level.SEVERE, null, ex);
734     }
735 }