OSDN Git Service

loggerを修正した
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / threads / ENThumbnailRunner.java
1 /*
2  * This file is part of NeighborNote
3  * Copyright 2013 Yuki Takahashi
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
8  * Software distributed under the License is distributed
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10  * express or implied. See the GPL for the specific language
11  * governing rights and limitations.
12  *
13  * You should have received a copy of the GPL along with this
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html
15  * or write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18 */
19
20 package cx.fbn.nevernote.threads;
21
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.LinkedBlockingQueue;
29
30 import org.apache.http.HttpResponse;
31 import org.apache.http.NameValuePair;
32 import org.apache.http.client.ClientProtocolException;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.entity.UrlEncodedFormEntity;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.impl.client.DefaultHttpClient;
37 import org.apache.http.message.BasicNameValuePair;
38 import org.apache.http.util.EntityUtils;
39
40 import com.evernote.edam.type.User;
41 import com.trolltech.qt.core.QByteArray;
42 import com.trolltech.qt.core.QMutex;
43 import com.trolltech.qt.core.QObject;
44 import com.trolltech.qt.gui.QPixmap;
45
46 import cx.fbn.nevernote.Global;
47 import cx.fbn.nevernote.oauth.OAuthTokenizer;
48 import cx.fbn.nevernote.signals.ENThumbnailSignal;
49 import cx.fbn.nevernote.signals.LimitSignal;
50 import cx.fbn.nevernote.sql.DatabaseConnection;
51 import cx.fbn.nevernote.utilities.AESEncrypter;
52 import cx.fbn.nevernote.utilities.ApplicationLogger;
53
54 public class ENThumbnailRunner extends QObject implements Runnable{
55         
56         private final ApplicationLogger                                 logger;
57         private final DatabaseConnection                                conn;
58         public volatile ENThumbnailSignal                               enThumbnailSignal;
59         public QMutex                                                                   mutex;
60         private volatile boolean                                                keepRunning;
61         private volatile LinkedBlockingQueue<String>    workQueue;
62         public volatile LimitSignal                                     limitSignal;
63         private volatile User                                                   user;
64         private volatile String                                                 serverUrl;
65         
66         public ENThumbnailRunner(String logname, int t, String u, String i, String r, String b, String uid, String pswd, String cpswd) {
67                 this.logger = new ApplicationLogger(logname);
68                 this.conn = new DatabaseConnection(logger, u, i, r, b, uid, pswd, cpswd, 0);
69                 this.enThumbnailSignal = new ENThumbnailSignal();
70                 this.mutex = new QMutex();
71                 this.keepRunning = true;
72                 this.workQueue = new LinkedBlockingQueue<String>();
73                 this.limitSignal = new LimitSignal();
74                 this.user = new User();
75                 this.serverUrl = "";
76         }
77
78         @Override
79         public void run() {
80                 thread().setPriority(Thread.MIN_PRIORITY);
81                 
82                 logger.log(logger.MEDIUM, "ENThumbnailスレッド開始");
83                 while (keepRunning) {
84                         try {
85                                 String work = workQueue.take();
86                                 mutex.lock();
87                                 if (work.startsWith("GET")) {
88                                         String guid = work.replace("GET ", "");
89                                         logger.log(logger.EXTREME, "Evernoteサムネイル取得開始 guid = " + guid);
90                                         
91                                         QByteArray thumbnailData = getENThumbnailData(guid);
92                                         if (thumbnailData == null) {                            // 取得に失敗
93                                                 logger.log(logger.EXTREME, "Evernoteサムネイルの取得に失敗");
94                                         } else {
95                                                 QPixmap thumbnail_p = new QPixmap();
96                                                 thumbnail_p.loadFromData(thumbnailData);
97                                                 logger.log(logger.EXTREME, "Evernoteサムネイルの取得に成功");
98                                                 saveImage(thumbnail_p, guid);
99                                                 registImage(thumbnailData, guid);
100                                         }
101                                         
102                                         enThumbnailSignal.getENThumbnailFinished.emit(guid);
103                                         logger.log(logger.EXTREME, "Evernoteサムネイル取得完了 guid = " + guid);
104                                 } else if (work.startsWith("STOP")) {
105                                         logger.log(logger.MEDIUM, "ENThumbnailスレッド停止");
106                                         keepRunning = false;
107                                 }
108                                 mutex.unlock();
109                         } catch (InterruptedException e) {
110                                 // TODO 自動生成された catch ブロック
111                                 e.printStackTrace();
112                         }
113                 }
114         }
115         
116         // Evernoteサーバからサムネイルを取得
117         private synchronized QByteArray getENThumbnailData(String guid) {
118                 // サムネイルをEvernoteサーバから取得
119                 String shardId = user.getShardId();
120                 if (shardId == null || shardId.equals("")) {
121                         return null;
122                 }
123                 
124                 OAuthTokenizer tokenizer = new OAuthTokenizer();
125         AESEncrypter aes = new AESEncrypter();
126         try {
127                         aes.decrypt(new FileInputStream(Global.getFileManager().getHomeDirFile("oauth.txt")));
128                 } catch (FileNotFoundException e) {
129                         logger.log(logger.HIGH, "Evernoteサムネイル取得中に例外発生:FileNotFoundException");
130                         e.printStackTrace();
131                 }
132                 String authString = aes.getString();
133                 String oauthToken = new String();
134                 if (!authString.equals("")) {
135                         tokenizer.tokenize(authString);
136                         oauthToken = tokenizer.oauth_token;
137                 }
138                 
139                 HttpClient httpClient = new DefaultHttpClient();
140
141                 HttpPost httpPost = new HttpPost("https://" + serverUrl + "/shard/" + user.getShardId() + "/thm/note/" + guid + ".png");
142                 httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
143                 httpPost.setHeader("Host", getServerUrl());
144
145                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
146                 nameValuePairs.add(new BasicNameValuePair("auth", oauthToken));
147                 nameValuePairs.add(new BasicNameValuePair("size", "80"));
148                 
149                 QByteArray data = new QByteArray();
150                 try {
151                         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
152                         // Webサーバからのレスポンスを処理
153                         HttpResponse response = null;
154                         response = httpClient.execute(httpPost);
155                         byte[] bytes = EntityUtils.toByteArray(response.getEntity());
156                         data = new QByteArray(bytes);
157                 } catch (UnsupportedEncodingException e) {
158                         logger.log(logger.HIGH, "Evernoteサムネイル取得中に例外発生:UnsupportedEncodingException");
159                         e.printStackTrace();
160                         return null;
161                 } catch (ClientProtocolException e) {
162                         logger.log(logger.HIGH, "Evernoteサムネイル取得中に例外発生:ClientProtocolException");
163                         e.printStackTrace();
164                         return null;
165                 } catch (IOException e) {
166                         logger.log(logger.HIGH, "Evernoteサムネイル取得中に例外発生:IOException");
167                         e.printStackTrace();
168                         return null;
169                 } finally {
170                         httpClient.getConnectionManager().shutdown();
171                 }
172
173                 return data;
174         }
175         
176         // サムネイルをpng形式のファイルとしてresディレクトリに保存
177         private synchronized void saveImage(QPixmap thumbnail, String guid) {
178                 String thumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + guid + ".png");
179                 thumbnail.save(thumbnailName, "PNG");
180         }
181         
182         // サムネイルのバイナリデータをデータベースに登録
183         private synchronized void registImage(QByteArray data, String guid) {
184                 conn.getNoteTable().setENThumbnail(guid, data);
185         }
186
187         public boolean isKeepRunning() {
188                 return keepRunning;
189         }
190
191         public void setKeepRunning(boolean keepRunning) {
192                 this.keepRunning = keepRunning;
193         }
194         
195         public boolean addGuid(String guid) {
196                 if (workQueue.offer("GET " + guid)) {
197                         return true;
198                 }
199                 
200                 return false;
201         }
202         
203         public boolean addStop() {
204                 if (workQueue.offer("STOP")) {
205                         return true;
206                 }
207                 return false;
208         }
209
210         public User getUser() {
211                 return user;
212         }
213
214         public void setUser(User user) {
215                 this.user = user;
216         }
217
218         public String getServerUrl() {
219                 return serverUrl;
220         }
221
222         public void setServerUrl(String serverUrl) {
223                 this.serverUrl = serverUrl;
224         }
225 }