1 package net.sf.jhylafax;
2
3 import java.io.File;
4 import java.util.Timer;
5 import java.util.TimerTask;
6 import net.sf.jhylafax.JobHelper.StatusResponse;
7 import net.sf.jhylafax.JobHelper.StatusUpdateJob;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.xnap.commons.io.Job;
11 import org.xnap.commons.io.NullProgressMonitor;
12
13
14 public class NotificationTimer {
15
16 private final static Log logger = LogFactory.getLog(NotificationTimer.class);
17 private Timer timer;
18 private TimerTask monitorPathTask;
19 private StatusUpdateTask statusUpdateTask;
20
21 public NotificationTimer()
22 {
23 timer = new Timer();
24 }
25
26 public void cancel()
27 {
28 timer.cancel();
29 }
30
31 public void settingsUpdated()
32 {
33 if (monitorPathTask != null) {
34 monitorPathTask.cancel();
35 monitorPathTask = null;
36 }
37
38 if (Settings.DO_MONITOR_PATH.getValue()) {
39 monitorPathTask = new MonitorPathTask(Settings.MONITOR_PATH.getValue());
40 timer.schedule(monitorPathTask, 0, Settings.MONITOR_PATH_INTERVAL.getValue() * 1000);
41 }
42
43 if (statusUpdateTask != null) {
44 statusUpdateTask.cancel();
45 statusUpdateTask = null;
46 }
47
48 if (Settings.DO_AUTO_UPDATE.getValue()) {
49 statusUpdateTask = new StatusUpdateTask();
50 timer.schedule(statusUpdateTask, 0, Settings.AUTO_UPDATE_INTERVAL.getValue() * 1000);
51 }
52
53 }
54
55 private class MonitorPathTask extends TimerTask {
56
57 private File file;
58 private long lastUpdate;
59
60 public MonitorPathTask(String path)
61 {
62 this.file = new File(path);
63 this.lastUpdate = file.lastModified();
64 }
65
66 @Override
67 public void run()
68 {
69 long check = file.lastModified();
70 if (check != lastUpdate) {
71 JHylaFAX.getInstance().runNotification(new Notification() {
72 public void run()
73 {
74 SendDialog dialog = new SendDialog(JHylaFAX.getInstance());
75 dialog.setDocument(file.getAbsolutePath());
76 dialog.setLocationRelativeTo(JHylaFAX.getInstance());
77 dialog.setVisible(true);
78 }
79 });
80 this.lastUpdate = check;
81 }
82 }
83
84 }
85
86 private class StatusUpdateTask extends TimerTask {
87
88 @Override
89 public void run()
90 {
91 Job<StatusResponse> ioJob = new StatusUpdateJob();
92 try {
93 StatusResponse response = ioJob.run(new NullProgressMonitor());
94 JHylaFAX.getInstance().updateTables(response);
95 }
96 catch (Exception e) {
97 logger.warn("Error during auto status update", e);
98 }
99 }
100
101
102 }
103
104 }