Source code: plugins/YabbifierService/YabbifierService.java
1 /*
2 This file is part of DeXter - Java Internet Communication Solution
3 Copyright (c) 2002 Tobias Riemer
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * YabbifierService.java
22 *
23 * Created on 17. September 2002, 22:33
24 */
25
26 package plugins.YabbifierService;
27
28 /**
29 *
30 * @author Tobias Riemer
31 */
32
33 import java.util.*;
34 import java.net.*;
35 import java.io.*;
36 import javax.swing.*;
37 import java.awt.*;
38 import java.awt.event.*;
39 import javax.swing.tree.*;
40 import java.util.HashMap;
41 import java.io.*;
42 import java.net.*;
43 import java.util.Vector;
44
45 import dexter.core.*;
46 import dexter.property.*;
47 import dexter.swingExtensions.*;
48
49 public class YabbifierService extends DefaultService {
50
51 /** Creates a new instance of YabbifierService */
52
53 Vector nodes = new Vector();
54 Vector msg = new Vector();
55 Property pUrl = new Property("Url", Property.STRING_VALUE, "http://", true);
56 Property pSleep = new Property("Sleep",Property.INT_VALUE, 10000);
57 Property pBrowser = new Property("Browser", Property.STRING_VALUE, true);
58 javax.swing.ImageIcon rolloverIcon = new javax.swing.ImageIcon(getClass().getResource("/dexter/images/yabbifier.gif"));
59
60 public YabbifierService() {
61 super("Yabbifier");
62
63 setServiceName("Yabbifier");
64
65 setIcon(new javax.swing.ImageIcon(getClass().getResource("/dexter/images/yabbifier.gif")));
66
67 root = new DefaultMutableTreeNode(this);
68
69 PropertyGroup pg = new PropertyGroup("General", new javax.swing.ImageIcon(getClass().getResource("/dexter/images/yabb2.gif")));
70 propertyFile.addGroup(pg);
71 pg.addProperty(pUrl);
72 pg.addProperty(pBrowser);
73 pg.addProperty(pSleep);
74
75 propertyFile.setFileName(Dexter.getInstance().getHomeDir() + "Yabbifier");
76
77 setToolTipText(getUrl());
78 }
79
80 public void initRun() {
81 super.initRun();
82 loadMsg();
83 for (int i=0;i<10;i++) {
84 DefaultMutableTreeNode node = new DefaultMutableTreeNode(msg.get(i));
85 nodes.add(node);
86 fireNodeAdded(root,node);
87 }
88 }
89
90 public void doRun() {
91 URLConnection conn = null;
92 BufferedReader data = null;
93 String line;
94 StringBuffer buf = new StringBuffer();
95 try {
96 URL url = new URL(getUrl());
97 conn = url.openConnection();
98 conn.connect();
99 data = new BufferedReader(new InputStreamReader(conn.getInputStream()));
100 String token = "<td width=75% bgcolor=\"#6E94B7\"><font class=\"text1\" color=\"#FFFFFF\" size=2>";
101 String token2 = "<font class=\"text1\" color=\"#FFFFFF\" size=2><u>";
102 String token3 = "</u></font></a></b></font></td>";
103 String token4 = "<a href=\""; String token5 = "\"";
104
105 int i = 0;
106 while ((line = data.readLine()) != null) {
107 //buf.append(line + "\n");
108
109 int index = line.indexOf(token);
110 if (index >= 0) {
111 int uind = line.indexOf(token4,index+token.length());
112 int uind2 = line.indexOf(token5,uind+token4.length());
113 index = line.indexOf(token2,index+token.length());
114
115 if (index >= 0) {
116 int index2 = line.indexOf(token3,index+token2.length());
117 String yName = line.substring(index+token2.length(),index2);
118 String yUrl = line.substring(uind+token4.length(),uind2);
119
120 if (i<10) {
121 YabbMsg m = (YabbMsg) msg.get(i);
122 if (!m.getMsg().equals(yName)) {
123 rotateMsg(msg,i);
124 DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.get(i);
125 msg.set(i,new YabbMsg(yName,yUrl,getBrowser()));
126 node.setUserObject(msg.get(i));
127 fireNodeChanged(node);
128 } else {
129 if (!m.getUrl().equals(yUrl)) {
130 m.setRead(false);
131 DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.get(i);
132 fireNodeChanged(node);
133 }
134 }
135 i++;
136 }
137 }
138 }
139 }
140 //System.out.println(buf.toString());
141 data.close();
142 }
143 catch (IOException e) {
144 e.printStackTrace();
145 }
146
147 int readCnt = 0;
148 for(int i=0;i<msg.size();i++) {
149 if(! ((YabbMsg) msg.get(i)).isRead() ) readCnt ++;
150 }
151 setServiceName("Yabbifier (" + readCnt + ")");
152 fireNodeChanged(root);
153
154 try {
155 thread.sleep(getSleep());
156 } catch(Exception ex) {}
157 }
158
159
160 public void finalizeRun() {
161 super.finalizeRun();
162 saveMsg();
163 }
164
165 protected void rotateMsg(Vector msg, int n) {
166 // for(int i=0;i<10;i++) System.out.println( ((YabbMsg)msg.get(i)).getMsg() + " " + ((YabbMsg)msg.get(i)).getUrl());
167 for(int i=9;i>n;i--) {
168 msg.set(i,msg.get(i-1));
169 }
170 // for(int i=0;i<10;i++) System.out.println( ((YabbMsg)msg.get(i)).getMsg() + " " + ((YabbMsg)msg.get(i)).getUrl());
171 }
172
173 public int getSleep() {
174 return pSleep.intValue();
175 }
176
177 public String getUrl() {
178 return pUrl.stringValue() + "?action=recent";
179 }
180
181 public String getBrowser() {
182 return pBrowser.stringValue();
183 }
184
185 public void loadMsg() {
186
187 try {
188
189 BufferedReader in = new BufferedReader(new FileReader(Dexter.getInstance().getHomeDir() + "yabbmsg.dat"));
190 String str;
191
192 while ((str = in.readLine()) != null) {
193 YabbMsg m = new YabbMsg("","",getBrowser());
194 msg.add(m);
195 m.read(str);
196 }
197 in.close();
198 } catch (IOException e) {
199 }
200 while (msg.size()<10) {
201 msg.add(new YabbMsg("","",getBrowser()));
202 }
203 }
204
205 public void saveMsg() {
206 try {
207
208 BufferedWriter out = new BufferedWriter(new FileWriter(Dexter.getInstance().getHomeDir() + "yabbmsg.dat"));
209 String str;
210
211 for (int i=0;i<msg.size();i++) {
212 out.write(((YabbMsg)msg.get(i)).write());
213 out.newLine();
214 }
215 out.flush();
216 out.close();
217 } catch (IOException e) {
218 e.printStackTrace();
219 }
220 }
221
222 public JPopupMenu getTrayPopupMenu() {
223 JPopupMenu pm = new JPopupMenu();
224 for (int i=0;i<msg.size();i++) {
225 YabbMsg ym = (YabbMsg) msg.get(i);
226 JMenuItem m = new JMenuItem(ym.getMsg());
227 pm.add(m);
228 }
229
230 return pm;
231 }
232
233
234 }