OSDN Git Service

キーワード検索ダイアログ等にESCキーでのキャンセル動作を追加
[tainavi/TinyBannavi.git] / TinyBannavi / src / tainavi / PlugIn_RecRD_MAIL.java
1 package tainavi;\r
2 \r
3 import java.io.BufferedReader;\r
4 import java.io.File;\r
5 import java.io.FileNotFoundException;\r
6 import java.io.FileReader;\r
7 import java.io.IOException;\r
8 import java.io.UnsupportedEncodingException;\r
9 import java.util.Calendar;\r
10 import java.util.Date;\r
11 import java.util.GregorianCalendar;\r
12 import java.util.Properties;\r
13 \r
14 import javax.mail.AuthenticationFailedException;\r
15 import javax.mail.Authenticator;\r
16 import javax.mail.Message;\r
17 import javax.mail.MessagingException;\r
18 import javax.mail.PasswordAuthentication;\r
19 import javax.mail.Session;\r
20 import javax.mail.Transport;\r
21 import javax.mail.internet.InternetAddress;\r
22 import javax.mail.internet.MimeMessage;\r
23 \r
24 \r
25 public class PlugIn_RecRD_MAIL extends HDDRecorderUtils implements HDDRecorder,Cloneable {\r
26 \r
27         public PlugIn_RecRD_MAIL clone() {\r
28                 return (PlugIn_RecRD_MAIL) super.clone();\r
29         }\r
30         \r
31         /* 必須コード  - ここから */\r
32         \r
33         // 種族の特性\r
34         public String getRecorderId() { return "RD(Mail)"; }\r
35         public RecType getType() { return RecType.MAIL; }\r
36         \r
37         // 個体の特性\r
38         private ChannelCode cc = new ChannelCode(getRecorderId());\r
39         private String rsvedFile = "";\r
40         \r
41         private String errmsg = "";\r
42 \r
43         protected String getDefFile() { return "env/mail.def"; }\r
44 \r
45         // 公開メソッド\r
46         \r
47         /*\r
48          * \r
49          */\r
50         public String Myself() {\r
51                 return("MAIL"+":"+getMacAddr()+":"+getRecorderId());\r
52         }\r
53         public ChannelCode getChCode() {\r
54                 return cc;\r
55         }\r
56         \r
57         // 繰り返し予約をサポートしていない\r
58         @Override\r
59         public boolean isRepeatReserveSupported() { return false; }\r
60         \r
61         /*\r
62          * \r
63          */\r
64         public boolean ChangeChannel(String Channel) {\r
65                 return false;\r
66         }\r
67 \r
68         /*\r
69          *      レコーダーから予約一覧を取得する \r
70          */\r
71         public boolean GetRdReserve(boolean force)\r
72         {\r
73                 errmsg = "";\r
74                 \r
75                 System.out.println("Through: GetRdReserve("+force+")");\r
76                 \r
77                 String defFile = getDefFile();\r
78                 \r
79                 FileReader fr = null;\r
80                 BufferedReader r = null;\r
81                 try {\r
82                         encoder.clear();\r
83                         vrate.clear();\r
84                         arate.clear();\r
85                         device.clear();\r
86                         dvdcompat.clear();\r
87                         aspect.clear();\r
88                         xchapter.clear();\r
89                         mschapter.clear();\r
90                         mvchapter.clear();\r
91                         \r
92                         fr = new FileReader(defFile);\r
93                         r = new BufferedReader(fr);\r
94                         String s ;\r
95                         while ( (s = r.readLine()) != null ) {\r
96                                 String[] b = s.split(",");\r
97                                 if ( b.length >= 3 ) {\r
98                                         TextValueSet t = new TextValueSet() ;\r
99                                         t.setText(b[1]) ;\r
100                                         t.setValue(b[2]) ;\r
101                                         \r
102                                         if ( b[0].equals("7") ) {\r
103                                                 encoder.add(t) ;\r
104                                         }\r
105                                         else if ( b[0].equals("10") ) {\r
106                                                 vrate.add(t) ;\r
107                                         }\r
108                                         else if ( b[0].equals("11") ) {\r
109                                                 arate.add(t) ;\r
110                                         }\r
111                                         else if ( b[0].equals("12") ) {\r
112                                                 device.add(t) ;\r
113                                         }\r
114                                         else if ( b[0].equals("14") ) {\r
115                                                 dvdcompat.add(t) ;\r
116                                         }\r
117                                         else if ( b[0].equals("16") ) {\r
118                                                 aspect.add(t) ;\r
119                                         }\r
120                                         else if ( b[0].equals("17") ) {\r
121                                                 xchapter.add(t) ;\r
122                                         }\r
123                                         else if ( b[0].equals("18") ) {\r
124                                                 mschapter.add(t) ;\r
125                                         }\r
126                                         else if ( b[0].equals("19") ) {\r
127                                                 mvchapter.add(t) ;\r
128                                         }\r
129                                 }\r
130                         }\r
131                 } catch (FileNotFoundException e) {\r
132                         // TODO Auto-generated catch block\r
133                         e.printStackTrace();\r
134                 } catch (IOException e) {\r
135                         // TODO Auto-generated catch block\r
136                         e.printStackTrace();\r
137                 }\r
138                 finally {\r
139                         if (r != null) try { r.close(); } catch (Exception e) {};\r
140                         if (fr != null) try { fr.close(); } catch (Exception e) {};\r
141                 }\r
142                 \r
143                 // 予約一覧をキャッシュから\r
144                 rsvedFile = "env/reserved."+getIPAddr()+"_"+getPortNo()+"_"+getRecorderId()+".xml";\r
145                 \r
146                 File f = new File(rsvedFile);\r
147                 if ( force == false && f.exists()) {\r
148                         // キャッシュから読み出し(予約一覧)\r
149                         setReserves(ReservesFromFile(rsvedFile));\r
150                         if (getReserves().size() > 0) {\r
151                                 System.out.println("+read from="+rsvedFile);\r
152                         }\r
153                         return(true);\r
154                 }\r
155                 \r
156                 return(true);\r
157         }\r
158         \r
159         /*\r
160          *      予約を実行する\r
161          */\r
162         public boolean PostRdEntry(ReserveList r)\r
163         {\r
164                 errmsg = "";\r
165                 \r
166                 //\r
167                 if (cc.getCH_WEB2CODE(r.getCh_name()) == null) {\r
168                         errmsg = "【警告】Web番組表の放送局名「"+r.getCh_name()+"」をCHコードに変換できません。CHコード設定を修正してください。" ;\r
169                         System.out.println(errmsg);\r
170                         return(false);\r
171                 }\r
172                 \r
173                 System.out.println("Run: PostRdEntry("+r.getTitle()+")");\r
174 \r
175                 // メール本文を作る\r
176                 String msg = "";\r
177                 String message = "";\r
178                 \r
179                 GregorianCalendar c = CommonUtils.getCalendar(r.getRec_pattern());\r
180                 if (c == null) {\r
181                         errmsg = "日付指定しか利用出来ません。" ;\r
182                         return(false) ;\r
183                 }\r
184                 msg = getMailBody(r, this.getBroadcast());\r
185                 \r
186                 try {\r
187                         message = new String(msg.getBytes("MS932"),"Shift_JIS");\r
188                 } catch (UnsupportedEncodingException e1) {\r
189                         // TODO Auto-generated catch block\r
190                         e1.printStackTrace();\r
191                 }\r
192                 \r
193                 // メールを送信する\r
194                 try {\r
195                         Properties props = new Properties() ;\r
196                         props.put("mail.smtp.host", this.getIPAddr()) ;\r
197                         props.put("mail.host", this.getIPAddr()) ;\r
198                         props.put("mail.smtp.port", this.getPortNo()) ;\r
199                         props.put("mail.smtp.auth", "true") ;\r
200                         props.put("mail.smtp.starttls.enable","true");\r
201                         \r
202                         Session session = Session.getInstance( props, new Authenticator() {\r
203                                 protected PasswordAuthentication getPasswordAuthentication() {\r
204                                         return new PasswordAuthentication(getUser(), getPasswd()) ;\r
205                                 }\r
206                         });\r
207                         //session.setDebug(true);\r
208                         MimeMessage mimeMessage = new MimeMessage(session);\r
209                         mimeMessage.setRecipients(Message.RecipientType.TO, this.getMacAddr());\r
210                         mimeMessage.setFrom(new InternetAddress(this.getUser()));\r
211                         mimeMessage.setSubject(new Date().toString());\r
212                         mimeMessage.setSentDate(new Date());\r
213                         //mimeMessage.setText(message, "ISO-2022-JP");\r
214                         mimeMessage.setContent(message, "text/html;charset=iso-2022-jp");\r
215                         mimeMessage.setHeader("Content-Transfer-Encoding", "7bit"); \r
216                         Transport.send(mimeMessage);\r
217         } catch (AuthenticationFailedException e) {\r
218                 errmsg = "SMTP認証に失敗しました:"+e.toString() ;\r
219                 return(false) ;\r
220         } catch (MessagingException e) {\r
221                 errmsg = "メール送信に失敗しました:"+e.toString() ;\r
222                 return(false) ;\r
223                 }\r
224                 \r
225                 // 予約ID\r
226                 long no = 0;\r
227                 for (ReserveList x : getReserves()) {\r
228                         if (Long.valueOf(x.getId()) > no) {\r
229                                 no = Long.valueOf(x.getId());\r
230                         }\r
231                 }\r
232                 \r
233                 r.setId(String.valueOf(++no));\r
234                 // 予約パターンID\r
235                 r.setRec_pattern_id(getRec_pattern_Id(r.getRec_pattern()));\r
236                 \r
237                 // 次回予定日\r
238                 r.setRec_nextdate(CommonUtils.getNextDate(r));\r
239                 \r
240                 // 録画長\r
241                 r.setRec_min(CommonUtils.getRecMin(r.getAhh(),r.getAmm(),r.getZhh(),r.getZmm()));\r
242                 \r
243                 // 開始日時・終了日時\r
244                 getStartEndDateTime(r);\r
245                 \r
246                 // 予約リストを更新\r
247                 getReserves().add(r);\r
248                 \r
249                 // キャッシュに保存\r
250                 ReservesToFile(getReserves(), rsvedFile);\r
251                 \r
252                 return(true);\r
253         }\r
254         protected String getMailBody(ReserveList r, String passwd) {\r
255                 GregorianCalendar c = CommonUtils.getCalendar(r.getRec_pattern());\r
256                 \r
257                 StringBuilder sb = new StringBuilder();\r
258                 \r
259                 sb.append("open");\r
260                 sb.append(" ");\r
261                 sb.append(this.getBroadcast());\r
262                 sb.append(" ");\r
263                 sb.append("prog ");\r
264                 sb.append("add ");\r
265                 sb.append(String.format("%04d%02d%02d ",c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1,c.get(Calendar.DATE))+" ");\r
266                 sb.append(" ");\r
267                 sb.append(r.getAhh()+r.getAmm());\r
268                 sb.append(" ");\r
269                 sb.append(r.getZhh()+r.getZmm());\r
270                 sb.append(" ");\r
271                 sb.append(cc.getCH_WEB2CODE(r.getCh_name()));\r
272                 sb.append(" ");\r
273                 sb.append(text2value(encoder,r.getTuner()));\r
274                 sb.append(" ");\r
275                 sb.append(text2value(vrate,r.getRec_mode()));\r
276                 sb.append(" ");\r
277                 if (r.getRec_mode().indexOf("[TS") != 0) {\r
278                         sb.append(text2value(arate,r.getRec_audio()));\r
279                         sb.append(" ");\r
280                 }\r
281                 sb.append(text2value(device,r.getRec_device()));\r
282                 sb.append(" ");\r
283                 sb.append(text2value(dvdcompat,r.getRec_dvdcompat()));\r
284                 String chapter_mode = text2value(xchapter,r.getRec_xchapter());\r
285                 if (chapter_mode != null) {\r
286                         sb.append(" ");\r
287                         sb.append(chapter_mode);\r
288                 }\r
289                 chapter_mode = text2value(mvchapter,r.getRec_mvchapter());\r
290                 if (chapter_mode != null) {\r
291                         sb.append(" ");\r
292                         sb.append(chapter_mode);\r
293                 }\r
294                 else {\r
295                         sb.append(" ");\r
296                         sb.append("CPN");       // マジックチャプターOFFをつけないとエラー発生\r
297                 }\r
298                 chapter_mode = text2value(mschapter,r.getRec_mschapter());\r
299                 if (chapter_mode != null) {\r
300                         sb.append(" ");\r
301                         sb.append(chapter_mode);\r
302                 }\r
303                 sb.append(" ");\r
304                 sb.append((r.getExec())?("RY"):("RN"));\r
305                 sb.append("\r\n");\r
306                 sb.append(r.getTitle());\r
307                 sb.append("\r\n");\r
308                 \r
309                 return sb.toString();\r
310         }\r
311         \r
312         /*\r
313          *      予約を更新する\r
314          */\r
315         public boolean UpdateRdEntry(ReserveList o, ReserveList r) {\r
316                 System.out.println("Through: UpdateRdEntry()");\r
317                 \r
318                 errmsg = "更新処理は無効です。";\r
319 \r
320                 return(false);\r
321         }\r
322 \r
323         /*\r
324          *      予約を削除する\r
325          */\r
326         public ReserveList RemoveRdEntry(String delid) {\r
327                 errmsg = "";\r
328                 \r
329                 System.out.println("Through: RemoveRdEntry()");\r
330 \r
331                 // 削除対象を探す\r
332                 ReserveList rx = null;\r
333                 for (  ReserveList reserve : getReserves() )  {\r
334                         if (reserve.getId().equals(delid)) {\r
335                                 rx = reserve;\r
336                                 break;\r
337                         }\r
338                 }\r
339                 if (rx == null) {\r
340                         return(null);\r
341                 }\r
342                 \r
343                 // 予約リストを更新\r
344                 getReserves().remove(rx);\r
345                 \r
346                 // キャッシュに保存\r
347                 ReservesToFile(getReserves(), rsvedFile);\r
348                 \r
349                 errmsg = "鯛ナビのエントリは削除しました。REGZA上で実際のエントリを削除して下さい。";\r
350                 \r
351                 return(rx);\r
352         }\r
353         \r
354         /*\r
355          * \r
356          */\r
357         public String getErrmsg() {\r
358                 return(errmsg.replaceAll("\\\\r\\\\n", ""));\r
359         }\r
360 }\r