diff options
8 files changed, 280 insertions, 130 deletions
diff --git a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form index 3435493..91459ed 100644 --- a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form +++ b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form | |||
@@ -28,11 +28,11 @@ | |||
28 | <Component id="jLabel1" alignment="0" min="-2" max="-2" attributes="0"/> | 28 | <Component id="jLabel1" alignment="0" min="-2" max="-2" attributes="0"/> |
29 | <Group type="102" alignment="0" attributes="0"> | 29 | <Group type="102" alignment="0" attributes="0"> |
30 | <EmptySpace min="12" pref="12" max="12" attributes="0"/> | 30 | <EmptySpace min="12" pref="12" max="12" attributes="0"/> |
31 | <Group type="103" groupAlignment="0" max="-2" attributes="0"> | 31 | <Group type="103" groupAlignment="0" attributes="0"> |
32 | <Group type="102" alignment="0" attributes="1"> | 32 | <Group type="102" alignment="0" attributes="0"> |
33 | <Component id="jLabel3" min="-2" max="-2" attributes="0"/> | 33 | <Component id="jLabel3" min="-2" max="-2" attributes="0"/> |
34 | <EmptySpace max="-2" attributes="0"/> | 34 | <EmptySpace max="-2" attributes="0"/> |
35 | <Component id="jTextField1" max="32767" attributes="0"/> | 35 | <Component id="jTextField1" min="-2" pref="214" max="-2" attributes="0"/> |
36 | </Group> | 36 | </Group> |
37 | <Component id="jLabel2" alignment="0" min="-2" max="-2" attributes="0"/> | 37 | <Component id="jLabel2" alignment="0" min="-2" max="-2" attributes="0"/> |
38 | </Group> | 38 | </Group> |
diff --git a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java index 149bea1..fb05037 100644 --- a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java +++ b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java | |||
@@ -6,10 +6,9 @@ | |||
6 | 6 | ||
7 | package com.fourisland.instadisc; | 7 | package com.fourisland.instadisc; |
8 | 8 | ||
9 | import com.fourisland.instadisc.Item.SubscriptionFile; | ||
9 | import java.net.MalformedURLException; | 10 | import java.net.MalformedURLException; |
10 | import java.net.URL; | 11 | import java.net.URL; |
11 | import java.util.logging.Level; | ||
12 | import java.util.logging.Logger; | ||
13 | 12 | ||
14 | /** | 13 | /** |
15 | * | 14 | * |
@@ -80,11 +79,11 @@ public class AddSubscriptionForm extends javax.swing.JDialog { | |||
80 | .addComponent(jLabel1) | 79 | .addComponent(jLabel1) |
81 | .addGroup(layout.createSequentialGroup() | 80 | .addGroup(layout.createSequentialGroup() |
82 | .addGap(12, 12, 12) | 81 | .addGap(12, 12, 12) |
83 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) | 82 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
84 | .addGroup(layout.createSequentialGroup() | 83 | .addGroup(layout.createSequentialGroup() |
85 | .addComponent(jLabel3) | 84 | .addComponent(jLabel3) |
86 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | 85 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
87 | .addComponent(jTextField1)) | 86 | .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)) |
88 | .addComponent(jLabel2))) | 87 | .addComponent(jLabel2))) |
89 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | 88 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
90 | .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | 89 | .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
@@ -119,8 +118,9 @@ public class AddSubscriptionForm extends javax.swing.JDialog { | |||
119 | private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed | 118 | private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed |
120 | try { | 119 | try { |
121 | URL url = new URL(jTextField1.getText());//GEN-LAST:event_jButton2ActionPerformed | 120 | URL url = new URL(jTextField1.getText());//GEN-LAST:event_jButton2ActionPerformed |
121 | SubscriptionFile sf = new SubscriptionFile(url, jLabel4); | ||
122 | } catch (MalformedURLException ex) { | 122 | } catch (MalformedURLException ex) { |
123 | jLabel4.setText("Error: "); | 123 | jLabel4.setText("Error: Subscription URL is malformed"); |
124 | } | 124 | } |
125 | } | 125 | } |
126 | 126 | ||
diff --git a/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java b/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java index 13623c0..57ec799 100644 --- a/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java +++ b/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java | |||
@@ -36,7 +36,7 @@ public class Wrapper { | |||
36 | envConfig.setAllowCreate(true); | 36 | envConfig.setAllowCreate(true); |
37 | esConfig.setAllowCreate(true); | 37 | esConfig.setAllowCreate(true); |
38 | try { | 38 | try { |
39 | e = new Environment(new File(loc + "db"), envConfig); | 39 | e = new Environment(new File(loc), envConfig); |
40 | es = new EntityStore(e, "EntityStore", esConfig); | 40 | es = new EntityStore(e, "EntityStore", esConfig); |
41 | } catch (DatabaseException ex) { | 41 | } catch (DatabaseException ex) { |
42 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 42 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
@@ -54,37 +54,38 @@ public class Wrapper { | |||
54 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 54 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
55 | } | 55 | } |
56 | } | 56 | } |
57 | 57 | ||
58 | public static String getConfig(String key) | 58 | public static String getConfig(String key) { |
59 | { | 59 | synchronized (idConfig) { |
60 | try { | 60 | try { |
61 | return idConfig.get(key).getValue(); | 61 | return idConfig.get(key).getValue(); |
62 | } catch (DatabaseException ex) { | 62 | } catch (DatabaseException ex) { |
63 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 63 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
64 | return ""; | 64 | return ""; |
65 | } | ||
65 | } | 66 | } |
66 | } | 67 | } |
67 | 68 | ||
68 | public static void setConfig(String key, String value) | 69 | public static void setConfig(String key, String value) { |
69 | { | 70 | synchronized (idConfig) { |
70 | try { | 71 | try { |
71 | if (idConfig.contains(key)) { | 72 | if (idConfig.contains(key)) { |
72 | IDConfig temp = idConfig.get(key); | 73 | IDConfig temp = idConfig.get(key); |
73 | temp.setValue(value); | 74 | temp.setValue(value); |
74 | idConfig.put(temp); | 75 | idConfig.put(temp); |
75 | } else { | 76 | } else { |
76 | IDConfig temp = new IDConfig(); | 77 | IDConfig temp = new IDConfig(); |
77 | temp.setKey(key); | 78 | temp.setKey(key); |
78 | temp.setValue(value); | 79 | temp.setValue(value); |
79 | idConfig.put(temp); | 80 | idConfig.put(temp); |
81 | } | ||
82 | } catch (DatabaseException ex) { | ||
83 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | ||
80 | } | 84 | } |
81 | } catch (DatabaseException ex) { | ||
82 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | ||
83 | } | 85 | } |
84 | } | 86 | } |
85 | 87 | ||
86 | public static boolean containsOldVerID(Integer id) | 88 | public static boolean containsOldVerID(Integer id) { |
87 | { | ||
88 | try { | 89 | try { |
89 | return oldVerID.contains(id); | 90 | return oldVerID.contains(id); |
90 | } catch (DatabaseException ex) { | 91 | } catch (DatabaseException ex) { |
@@ -92,117 +93,134 @@ public class Wrapper { | |||
92 | return false; | 93 | return false; |
93 | } | 94 | } |
94 | } | 95 | } |
95 | 96 | ||
96 | public static int countOldVerID() | 97 | public static int countOldVerID() { |
97 | { | 98 | synchronized (oldVerID) { |
98 | try { | 99 | try { |
99 | return (int) oldVerID.count(); | 100 | return (int) oldVerID.count(); |
100 | } catch (DatabaseException ex) { | 101 | } catch (DatabaseException ex) { |
101 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 102 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
102 | return 0; | 103 | return 0; |
104 | } | ||
103 | } | 105 | } |
104 | } | 106 | } |
105 | 107 | ||
106 | public static void emptyOldVerID() | 108 | public static void emptyOldVerID() { |
107 | { | 109 | synchronized (oldVerID) { |
108 | try { | ||
109 | EntityCursor<OldVerID> ec = oldVerID.entities(); | ||
110 | try { | 110 | try { |
111 | Iterator<OldVerID> i = ec.iterator(); | 111 | EntityCursor<OldVerID> ec = oldVerID.entities(); |
112 | while (i.hasNext()) { | 112 | try { |
113 | i.remove(); | 113 | Iterator<OldVerID> i = ec.iterator(); |
114 | while (i.hasNext()) { | ||
115 | i.remove(); | ||
116 | } | ||
117 | } finally { | ||
118 | ec.close(); | ||
114 | } | 119 | } |
115 | } finally { | 120 | } catch (DatabaseException ex) { |
116 | ec.close(); | 121 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
117 | } | 122 | } |
118 | } catch (DatabaseException ex) { | ||
119 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | ||
120 | } | 123 | } |
121 | } | 124 | } |
122 | 125 | ||
123 | public static void addOldVerID(Integer id) | 126 | public static void addOldVerID(Integer id) { |
124 | { | 127 | synchronized (oldVerID) { |
125 | try { | 128 | try { |
126 | OldVerID temp = new OldVerID(); | 129 | OldVerID temp = new OldVerID(); |
127 | temp.setID(id); | 130 | temp.setID(id); |
128 | oldVerID.put(temp); | 131 | oldVerID.put(temp); |
129 | } catch (DatabaseException ex) { | 132 | } catch (DatabaseException ex) { |
130 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 133 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
134 | } | ||
131 | } | 135 | } |
132 | } | 136 | } |
133 | 137 | ||
134 | public static void addItem(Item m_item) | 138 | public static void addItem(Item m_item) { |
135 | { | 139 | synchronized (item) { |
136 | try { | 140 | try { |
137 | item.put(m_item); | 141 | item.put(m_item); |
138 | } catch (DatabaseException ex) { | 142 | } catch (DatabaseException ex) { |
139 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 143 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
144 | } | ||
140 | } | 145 | } |
141 | } | 146 | } |
142 | 147 | ||
143 | public static int countItem() | 148 | public static int countItem() { |
144 | { | 149 | synchronized (item) { |
145 | try { | 150 | try { |
146 | return (int) item.count(); | 151 | return (int) item.count(); |
147 | } catch (DatabaseException ex) { | 152 | } catch (DatabaseException ex) { |
148 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 153 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
149 | return 0; | 154 | return 0; |
155 | } | ||
150 | } | 156 | } |
151 | } | 157 | } |
152 | 158 | ||
153 | public static void dropFromTopItem() | 159 | public static void dropFromTopItem() { |
154 | { | 160 | synchronized (item) { |
155 | try { | 161 | try { |
156 | Integer[] keySet = (Integer[]) item.map().keySet().toArray(); | 162 | Integer[] keySet = (Integer[]) item.map().keySet().toArray(); |
157 | item.delete(keySet[0]); | 163 | item.delete(keySet[0]); |
158 | } catch (DatabaseException ex) { | 164 | } catch (DatabaseException ex) { |
159 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 165 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
166 | } | ||
160 | } | 167 | } |
161 | } | 168 | } |
162 | 169 | ||
163 | public static Item[] getAllItem() | 170 | public static Item[] getAllItem() { |
164 | { | 171 | synchronized (item) { |
165 | try { | 172 | try { |
166 | Iterator<Item> i = item.entities().iterator(); | 173 | Iterator<Item> i = item.entities().iterator(); |
167 | Item[] temp = new Item[0]; | 174 | Item[] temp = new Item[0]; |
168 | int len = 0; | 175 | int len = 0; |
169 | 176 | ||
170 | while (i.hasNext()) | 177 | while (i.hasNext()) { |
171 | { | 178 | Item[] temp2 = new Item[len + 1]; |
172 | Item[] temp2 = new Item[len+1]; | 179 | int j = 0; |
173 | int j=0; | 180 | for (j = 0; j < len; j++) { |
174 | for (j=0;j<len;j++) | 181 | temp2[j] = temp[j]; |
175 | { | 182 | } |
176 | temp2[j] = temp[j]; | 183 | temp2[len] = i.next(); |
184 | temp = temp2; | ||
177 | } | 185 | } |
178 | temp2[len] = i.next(); | 186 | |
179 | temp = temp2; | 187 | return temp; |
188 | } catch (DatabaseException ex) { | ||
189 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | ||
190 | return new Item[0]; | ||
180 | } | 191 | } |
181 | |||
182 | return temp; | ||
183 | } catch (DatabaseException ex) { | ||
184 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | ||
185 | return new Item[0]; | ||
186 | } | 192 | } |
187 | } | 193 | } |
188 | 194 | ||
189 | public static Subscription getSubscription(String url) | 195 | public static Subscription getSubscription(String url) { |
190 | { | 196 | synchronized (subscription) { |
191 | try { | 197 | try { |
192 | return subscription.get(url); | 198 | return subscription.get(url); |
193 | } catch (DatabaseException ex) { | 199 | } catch (DatabaseException ex) { |
194 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 200 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
195 | return null; | 201 | return null; |
202 | } | ||
196 | } | 203 | } |
197 | } | 204 | } |
198 | 205 | ||
199 | public static boolean existsSubscription(String url) | 206 | public static boolean existsSubscription(String url) { |
200 | { | 207 | synchronized (subscription) { |
201 | try { | 208 | try { |
202 | return subscription.contains(url); | 209 | return subscription.contains(url); |
203 | } catch (DatabaseException ex) { | 210 | } catch (DatabaseException ex) { |
204 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | 211 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); |
205 | return false; | 212 | return false; |
213 | } | ||
214 | } | ||
215 | } | ||
216 | |||
217 | public static void addSubscription(Subscription s) { | ||
218 | synchronized (subscription) { | ||
219 | try { | ||
220 | subscription.put(s); | ||
221 | } catch (DatabaseException ex) { | ||
222 | Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); | ||
223 | } | ||
206 | } | 224 | } |
207 | } | 225 | } |
208 | } | 226 | } |
diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java index dffb663..e891d32 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java | |||
@@ -16,7 +16,7 @@ import org.jdesktop.application.SingleFrameApplication; | |||
16 | public class InstaDiscApp extends SingleFrameApplication { | 16 | public class InstaDiscApp extends SingleFrameApplication { |
17 | 17 | ||
18 | public static TrayIcon ti; | 18 | public static TrayIcon ti; |
19 | 19 | ||
20 | /** | 20 | /** |
21 | * At startup create and show the main frame of the application. | 21 | * At startup create and show the main frame of the application. |
22 | */ | 22 | */ |
@@ -52,7 +52,17 @@ public class InstaDiscApp extends SingleFrameApplication { | |||
52 | } | 52 | } |
53 | 53 | ||
54 | Wrapper.init(db.getAbsolutePath()); | 54 | Wrapper.init(db.getAbsolutePath()); |
55 | if (!Wrapper.getConfig("initCheck").equals("done")) { | 55 | |
56 | boolean notInit = false; | ||
57 | try { | ||
58 | if (!Wrapper.getConfig("initCheck").equals("done")) { | ||
59 | notInit = true; | ||
60 | } | ||
61 | } catch (NullPointerException ex) { | ||
62 | notInit = true; | ||
63 | } | ||
64 | |||
65 | if (notInit) { | ||
56 | Thread th = new Thread(new FirstRunWizard()); | 66 | Thread th = new Thread(new FirstRunWizard()); |
57 | th.start(); | 67 | th.start(); |
58 | } else { | 68 | } else { |
diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java index d9a6c8a..36b2375 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java | |||
@@ -285,7 +285,8 @@ public class InstaDiscView extends FrameView { | |||
285 | }//GEN-LAST:event_jList1MouseClicked | 285 | }//GEN-LAST:event_jList1MouseClicked |
286 | 286 | ||
287 | private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed | 287 | private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed |
288 | // TODO add your handling code here: | 288 | AddSubscriptionForm asf = new AddSubscriptionForm(new JFrame(), true); |
289 | asf.setVisible(true); | ||
289 | }//GEN-LAST:event_jMenuItem1ActionPerformed | 290 | }//GEN-LAST:event_jMenuItem1ActionPerformed |
290 | 291 | ||
291 | // Variables declaration - do not modify//GEN-BEGIN:variables | 292 | // Variables declaration - do not modify//GEN-BEGIN:variables |
diff --git a/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java b/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java index 29914b9..1bb0c23 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java | |||
@@ -5,6 +5,7 @@ | |||
5 | 5 | ||
6 | package com.fourisland.instadisc.Item.Categories; | 6 | package com.fourisland.instadisc.Item.Categories; |
7 | 7 | ||
8 | import com.fourisland.instadisc.Database.Wrapper; | ||
8 | import com.fourisland.instadisc.Item.WellFormedItem; | 9 | import com.fourisland.instadisc.Item.WellFormedItem; |
9 | import java.util.HashMap; | 10 | import java.util.HashMap; |
10 | import javax.swing.Icon; | 11 | import javax.swing.Icon; |
@@ -30,7 +31,8 @@ public class Category { | |||
30 | 31 | ||
31 | public static boolean checkForRequiredSemantics(HashMap<String, String> headerMap) { | 32 | public static boolean checkForRequiredSemantics(HashMap<String, String> headerMap) { |
32 | boolean good = true; | 33 | boolean good = true; |
33 | if (headerMap.get("Category").equals("forum-post")) { | 34 | String category = Wrapper.getSubscription(headerMap.get("Subscription")).getCategory(); |
35 | if (category.equals("forum-post")) { | ||
34 | good = (good ? WellFormedItem.checkForRequiredHeader(headerMap, "forum") : false); | 36 | good = (good ? WellFormedItem.checkForRequiredHeader(headerMap, "forum") : false); |
35 | } | 37 | } |
36 | return good; | 38 | return good; |
diff --git a/client/trunk/src/com/fourisland/instadisc/Item/Item.java b/client/trunk/src/com/fourisland/instadisc/Item/Item.java index 71e6f06..5ed6fe9 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/Item.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/Item.java | |||
@@ -48,7 +48,7 @@ public class Item { | |||
48 | di.setAuthor(headerMap.get("Author")); | 48 | di.setAuthor(headerMap.get("Author")); |
49 | di.setURL(new URL(headerMap.get("URL")).toString()); | 49 | di.setURL(new URL(headerMap.get("URL")).toString()); |
50 | 50 | ||
51 | HashMap<String, String> temp = headerMap; | 51 | HashMap<String, String> temp = (HashMap<String, String>) headerMap.clone(); |
52 | temp.remove("ID"); | 52 | temp.remove("ID"); |
53 | temp.remove("Verification"); | 53 | temp.remove("Verification"); |
54 | temp.remove("Verification-ID"); | 54 | temp.remove("Verification-ID"); |
diff --git a/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java new file mode 100644 index 0000000..adca86e --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java | |||
@@ -0,0 +1,119 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | package com.fourisland.instadisc.Item; | ||
6 | |||
7 | import com.fourisland.instadisc.Database.Subscription; | ||
8 | import com.fourisland.instadisc.Database.Wrapper; | ||
9 | import java.io.IOException; | ||
10 | import java.io.InputStream; | ||
11 | import java.net.HttpURLConnection; | ||
12 | import java.net.URL; | ||
13 | import java.util.HashMap; | ||
14 | import java.util.logging.Level; | ||
15 | import java.util.logging.Logger; | ||
16 | import javax.swing.JLabel; | ||
17 | |||
18 | /** | ||
19 | * | ||
20 | * @author hatkirby | ||
21 | */ | ||
22 | public class SubscriptionFile { | ||
23 | |||
24 | public HashMap<String, String> headerMap; | ||
25 | |||
26 | public SubscriptionFile(URL url, JLabel status) { | ||
27 | status.setText("Checking...."); | ||
28 | try { | ||
29 | HttpURLConnection urc = (HttpURLConnection) url.openConnection(); | ||
30 | Thread th = new Thread(new SubscriptionFileThread(urc, status)); | ||
31 | th.start(); | ||
32 | } catch (IOException ex) { | ||
33 | Logger.getLogger(SubscriptionFile.class.getName()).log(Level.SEVERE, null, ex); | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | class SubscriptionFileThread implements Runnable { | ||
39 | |||
40 | HttpURLConnection urc; | ||
41 | JLabel status; | ||
42 | |||
43 | public SubscriptionFileThread(HttpURLConnection urc, JLabel status) { | ||
44 | this.urc = urc; | ||
45 | this.status = status; | ||
46 | } | ||
47 | |||
48 | public void run() { | ||
49 | InputStream is = null; | ||
50 | try { | ||
51 | is = urc.getInputStream(); | ||
52 | int[] buffer = new int[1000]; | ||
53 | int rs = 0; | ||
54 | int i = 0; | ||
55 | |||
56 | while (rs != -1) { | ||
57 | try { | ||
58 | rs = is.read(); | ||
59 | |||
60 | if (rs != -1) { | ||
61 | buffer[i] = rs; | ||
62 | } | ||
63 | i++; | ||
64 | } catch (IOException ex) { | ||
65 | Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | StringBuilder result = new StringBuilder(); | ||
70 | int j = 0; | ||
71 | for (j = 0; j < i; j++) { | ||
72 | result.append(Character.toString((char) buffer[j])); | ||
73 | } | ||
74 | |||
75 | String[] headers = result.toString().split("\n"); | ||
76 | HashMap<String, String> headerMap = new HashMap<String, String>(); | ||
77 | i = 0; | ||
78 | while (1 == 1) { | ||
79 | try { | ||
80 | String[] nameVal = headers[i].split(": "); | ||
81 | String name = nameVal[0]; | ||
82 | String value = nameVal[1].trim(); | ||
83 | headerMap.put(name, value); | ||
84 | } catch (Exception ex) { | ||
85 | break; | ||
86 | } | ||
87 | i++; | ||
88 | } | ||
89 | |||
90 | if (headerMap.containsKey("Subscription")) { | ||
91 | if (headerMap.containsKey("Title")) { | ||
92 | if (headerMap.containsKey("Category")) { | ||
93 | Subscription s = new Subscription(); | ||
94 | s.setURL(headerMap.get("Subscription")); | ||
95 | s.setTitle(headerMap.get("Title")); | ||
96 | s.setCategory(headerMap.get("Category")); | ||
97 | Wrapper.addSubscription(s); | ||
98 | |||
99 | status.setText("You've sucessfully subscribed to that website"); | ||
100 | } else { | ||
101 | status.setText("Error: Subscription file is not well-formed"); | ||
102 | } | ||
103 | } else { | ||
104 | status.setText("Error: Subscription file is not well-formed"); | ||
105 | } | ||
106 | } else { | ||
107 | status.setText("Error: Subscription file is not well-formed"); | ||
108 | } | ||
109 | } catch (IOException ex) { | ||
110 | Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); | ||
111 | } finally { | ||
112 | try { | ||
113 | is.close(); | ||
114 | } catch (IOException ex) { | ||
115 | Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | } | ||