OSDN Git Service

Merge remote-tracking branch 'origin/develop'
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / ShareNotebook.java
1 /*\r
2  * This file is part of NixNote/NeighborNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.dialog;\r
21 \r
22 //**********************************************\r
23 //**********************************************\r
24 //* Share a notebook with another user.\r
25 //**********************************************\r
26 //**********************************************\r
27 \r
28 import java.util.ArrayList;\r
29 import java.util.List;\r
30 \r
31 import com.evernote.edam.error.EDAMErrorCode;\r
32 import com.evernote.edam.error.EDAMNotFoundException;\r
33 import com.evernote.edam.error.EDAMSystemException;\r
34 import com.evernote.edam.error.EDAMUserException;\r
35 import com.evernote.edam.type.Notebook;\r
36 import com.evernote.edam.type.SharedNotebook;\r
37 import com.evernote.thrift.TException;\r
38 import com.trolltech.qt.core.QModelIndex;\r
39 import com.trolltech.qt.core.Qt.ItemFlag;\r
40 import com.trolltech.qt.core.Qt.ItemFlags;\r
41 import com.trolltech.qt.gui.QAbstractItemView.SelectionBehavior;\r
42 import com.trolltech.qt.gui.QAbstractItemView.SelectionMode;\r
43 import com.trolltech.qt.gui.QApplication;\r
44 import com.trolltech.qt.gui.QDialog;\r
45 import com.trolltech.qt.gui.QFontMetrics;\r
46 import com.trolltech.qt.gui.QHBoxLayout;\r
47 import com.trolltech.qt.gui.QIcon;\r
48 import com.trolltech.qt.gui.QLabel;\r
49 import com.trolltech.qt.gui.QMessageBox;\r
50 import com.trolltech.qt.gui.QPushButton;\r
51 import com.trolltech.qt.gui.QTableWidget;\r
52 import com.trolltech.qt.gui.QTableWidgetItem;\r
53 import com.trolltech.qt.gui.QVBoxLayout;\r
54 \r
55 import cx.fbn.nevernote.sql.DatabaseConnection;\r
56 import cx.fbn.nevernote.threads.SyncRunner;\r
57 \r
58 public class ShareNotebook extends QDialog {\r
59         private final QPushButton                               okButton;\r
60         private final QPushButton                               addButton;\r
61         private final QPushButton                               deleteButton;\r
62         private boolean                                                 okClicked;\r
63         public final QTableWidget                               table;\r
64         private final List<SharedNotebook>              notebooks;\r
65         private final DatabaseConnection                conn;\r
66         private final Notebook                                  notebook;\r
67         private final SyncRunner                                syncRunner;\r
68         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
69         \r
70         public ShareNotebook(String guid, DatabaseConnection c, Notebook n,\r
71                                                 SyncRunner s) {\r
72                 setWindowIcon(new QIcon(iconPath+"globe.png"));\r
73                 okClicked = false;\r
74                 conn = c;\r
75                 syncRunner = s;;\r
76                 notebook = n;\r
77                 \r
78                 notebooks = conn.getSharedNotebookTable().getForNotebook(n.getGuid());\r
79                 okButton = new QPushButton();\r
80                 okButton.setText(tr("OK"));\r
81                 okButton.pressed.connect(this, "onClicked()");\r
82                 \r
83                 QHBoxLayout horizontalLayout = new QHBoxLayout();\r
84                 QHBoxLayout buttonLayout = new QHBoxLayout();\r
85                 buttonLayout.addStretch(1);\r
86                 buttonLayout.addWidget(okButton);\r
87                 setWindowTitle(tr("Share Notebook \"") +notebook.getName() + tr("\" With Others"));     \r
88                 \r
89                 table = new QTableWidget(notebooks.size(),3);\r
90                 List<String> headers = new ArrayList<String>();\r
91                 headers.add(tr("Email"));\r
92                 headers.add(tr("Access"));\r
93                 headers.add(tr("Login Required"));\r
94                 table.setHorizontalHeaderLabels(headers);\r
95                 table.verticalHeader().setVisible(false);\r
96                 table.setAlternatingRowColors(true);\r
97                 table.setSelectionBehavior(SelectionBehavior.SelectRows);\r
98                 table.setSelectionMode(SelectionMode.SingleSelection);\r
99                 table.itemSelectionChanged.connect(this, "tableSelection()");\r
100                 horizontalLayout.addWidget(table);\r
101                 \r
102                 \r
103                 addButton = new QPushButton();\r
104                 addButton.setText(tr("Add"));\r
105                 addButton.clicked.connect(this, "addPressed()");\r
106                 \r
107                 deleteButton = new QPushButton();\r
108                 deleteButton.setText(tr("Delete"));\r
109                 deleteButton.setEnabled(false);\r
110                 deleteButton.clicked.connect(this, "deletePressed()");\r
111                 \r
112                 QVBoxLayout editLayout = new QVBoxLayout();\r
113                 editLayout.addWidget(addButton);\r
114                 editLayout.addWidget(deleteButton);\r
115                 \r
116                 QHBoxLayout listLayout = new QHBoxLayout();\r
117                 listLayout.addLayout(horizontalLayout);\r
118                 listLayout.addLayout(editLayout);\r
119                 \r
120                 QVBoxLayout mainLayout = new QVBoxLayout();\r
121                 if (syncRunner.authToken == null) {\r
122                         QLabel msg = new QLabel(tr("You must be connected to make changes."));\r
123                         mainLayout.addWidget(msg);\r
124                 }\r
125                 mainLayout.addLayout(listLayout);\r
126                 mainLayout.addSpacing(1);\r
127                 mainLayout.addLayout(buttonLayout);\r
128                 setLayout(mainLayout);\r
129                 \r
130                 table.setColumnWidth(0, 160);\r
131                 resize(500, 200);\r
132                 load();\r
133                 \r
134                 if (syncRunner.authToken == null) {\r
135                         addButton.setEnabled(false);\r
136                         deleteButton.setEnabled(false);\r
137                 }\r
138                 \r
139         }\r
140         \r
141         @SuppressWarnings("unused")\r
142         private void onClicked() {\r
143                 okClicked = true;\r
144                 close();\r
145         }\r
146         \r
147 \r
148         public boolean okClicked() {\r
149                 return okClicked;\r
150         }\r
151         \r
152         @SuppressWarnings("unused")\r
153         private void itemSelected() {\r
154                 okButton.setEnabled(true);\r
155         }\r
156         \r
157         private void load() {\r
158                 for (int i=0; i<notebooks.size(); i++) {\r
159                         addRow(i, notebooks.get(i).getEmail(), \r
160                                          notebooks.get(i).isNotebookModifiable(),\r
161                                          notebooks.get(i).isRequireLogin());\r
162                 }\r
163         }\r
164 \r
165         private void addRow(int row, String email, boolean mod, boolean login) {\r
166                 QFontMetrics f = QApplication.fontMetrics();\r
167                 int fontHeight = f.height();\r
168 \r
169                 QTableWidgetItem emailWidget = new QTableWidgetItem();\r
170                 emailWidget.setText(email);\r
171                 table.setItem(row, 0, emailWidget);\r
172                 table.setRowHeight(row, fontHeight);\r
173                 emailWidget.setToolTip(email);\r
174                 ItemFlags flags = emailWidget.flags();\r
175                 flags.clear(ItemFlag.ItemIsEditable);\r
176                 emailWidget.setFlags(flags);\r
177 \r
178                 QTableWidgetItem accessWidget = new QTableWidgetItem();\r
179                 if (mod)\r
180                         accessWidget.setText(tr("Modify"));\r
181                 else\r
182                         accessWidget.setText(tr("Read Only"));\r
183                 table.setItem(row, 1,accessWidget);\r
184                 accessWidget.setFlags(flags);\r
185 \r
186                 QTableWidgetItem loginWidget = new QTableWidgetItem();\r
187                 if (login)\r
188                         loginWidget.setText(tr("True"));\r
189                 else\r
190                         loginWidget.setText(tr("False"));\r
191                 table.setItem(row, 2, loginWidget);\r
192                 loginWidget.setFlags(flags);\r
193                 \r
194 \r
195         }\r
196         \r
197         @SuppressWarnings("unused")\r
198         private void tableSelection() {\r
199                 if (syncRunner.authToken != null)\r
200                         deleteButton.setEnabled(true);\r
201         }\r
202         \r
203         \r
204         @SuppressWarnings("unused")\r
205         private void addPressed() {\r
206 \r
207                 \r
208         }\r
209         \r
210         \r
211         @SuppressWarnings("unused")\r
212         private void deletePressed() {\r
213                 QModelIndex index = table.currentIndex();\r
214                 int row = index.row();\r
215                 \r
216                 QTableWidgetItem dirWidget = table.item(row, 0);\r
217                 String value = dirWidget.text();\r
218                 List<SharedNotebook> notebooks = conn.getSharedNotebookTable().getForNotebook(notebook.getGuid());\r
219                 \r
220                 List<Long> ids = new ArrayList<Long>();\r
221                 for (int i=0; i<notebooks.size(); i++) {\r
222                         if (notebooks.get(i).getEmail().equalsIgnoreCase(value)) {\r
223                                 ids.add(notebooks.get(i).getId());\r
224                         }\r
225                 }\r
226                 \r
227                 if (ids.size() > 0) {\r
228                         try {\r
229                                 syncRunner.localNoteStore.expungeSharedNotebooks(syncRunner.authToken, ids);\r
230                         } catch (EDAMUserException e) {\r
231                                 e.printStackTrace();\r
232                         } catch (EDAMNotFoundException e) {\r
233                                 e.printStackTrace();\r
234                         } catch (EDAMSystemException e) {\r
235                                 if (e.getErrorCode() == EDAMErrorCode.RATE_LIMIT_REACHED) {\r
236                                         QMessageBox.warning(this, tr("Rate limit reached"), tr("Evernote usage has been temporarily exceeded. Please try again in ") +  + e.getRateLimitDuration() + tr(" seconds."));\r
237                                 }\r
238                                 e.printStackTrace();\r
239                         } catch (TException e) {\r
240                                 e.printStackTrace();\r
241                         }\r
242                         for (int i=0; i<ids.size(); i++)\r
243                                 conn.getSharedNotebookTable().expungeNotebook(ids.get(i), false);\r
244                 }\r
245                 \r
246                 table.clear();\r
247                 load();\r
248                 if (table.rowCount() == 0) {\r
249                         deleteButton.setEnabled(false);\r
250                 }               \r
251         }\r
252 }\r