OSDN Git Service

9be49b79751630f5c32b4b827cbb61b99b46a69d
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / AccountDialog.java
1 /*\r
2  * This file is part of NixNote \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 import java.text.SimpleDateFormat;\r
24 \r
25 import com.evernote.edam.type.UserAttributes;\r
26 import com.trolltech.qt.gui.QApplication;\r
27 import com.trolltech.qt.gui.QColor;\r
28 import com.trolltech.qt.gui.QDialog;\r
29 import com.trolltech.qt.gui.QFontMetrics;\r
30 import com.trolltech.qt.gui.QFrame;\r
31 import com.trolltech.qt.gui.QGridLayout;\r
32 import com.trolltech.qt.gui.QGroupBox;\r
33 import com.trolltech.qt.gui.QHBoxLayout;\r
34 import com.trolltech.qt.gui.QIcon;\r
35 import com.trolltech.qt.gui.QLabel;\r
36 import com.trolltech.qt.gui.QPalette;\r
37 import com.trolltech.qt.gui.QPalette.ColorRole;\r
38 import com.trolltech.qt.gui.QPushButton;\r
39 import com.trolltech.qt.gui.QTextDocument;\r
40 import com.trolltech.qt.gui.QTextEdit;\r
41 import com.trolltech.qt.gui.QTextEdit.LineWrapMode;\r
42 import com.trolltech.qt.gui.QWidget;\r
43 \r
44 import cx.fbn.nevernote.Global;\r
45 \r
46 public class AccountDialog extends QDialog {\r
47     String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
48         private final QPushButton ok;\r
49         \r
50         // Constructor\r
51         public AccountDialog() {\r
52                 \r
53                 // Setup window layout, title, & icon\r
54                 setWindowTitle(tr("Account Information"));\r
55                 setWindowIcon(new QIcon(new QIcon(iconPath+"account.png")));\r
56                 QGridLayout grid = new QGridLayout();\r
57                 setLayout(grid);\r
58                 QLabel premium;\r
59                 if (Global.isPremium())\r
60                         premium = new QLabel(tr("Premium"));\r
61                 else\r
62                         premium = new QLabel(tr("Free"));\r
63                 \r
64                 String userName = Global.username;\r
65                 \r
66                 // calculate the upload amount\r
67                 Long uploadAmt = Global.getUploadAmount();\r
68                 Long uploadLimit = Global.getUploadLimit();\r
69                 Long uploadLimitEnd = Global.getUploadLimitEnd();\r
70                 Long pct;\r
71                 if (uploadLimit > 0)\r
72                         pct = uploadAmt*100 / uploadLimit;\r
73                 else\r
74                         pct = new Long(0);\r
75                 String unit = tr(" Bytes");\r
76                 \r
77                 if (uploadAmt > 0) {\r
78                         uploadAmt = uploadAmt/1024;\r
79                         unit = tr(" KB");\r
80                 }\r
81                 if (uploadAmt >= 1024) {\r
82                         uploadAmt = uploadAmt / 1024;\r
83                         unit = tr(" MB");\r
84                 }\r
85                 if (uploadLimit > 0)\r
86                         uploadLimit = uploadLimit/1024/1024;\r
87         \r
88                 //showing only date need\r
89                 String fmt = Global.getDateFormat() /* + " " + Global.getTimeFormat()*/;\r
90                 String dateTimeFormat = new String(fmt);\r
91                 SimpleDateFormat simple = new SimpleDateFormat(dateTimeFormat);\r
92                 StringBuilder endDate = new StringBuilder(simple.format(uploadLimitEnd));\r
93                 \r
94                 // Show limits\r
95                 QGroupBox limitGroup = new QGroupBox(tr("Account:"));\r
96 \r
97                 QGridLayout textGrid = new QGridLayout();\r
98                 textGrid.addWidget(new QLabel(tr("User Name:")),1,1);\r
99                 textGrid.addWidget(new QLabel(userName), 1,2);\r
100                 textGrid.addWidget(new QLabel(tr("Account Type:")), 2,1);\r
101                 textGrid.addWidget(premium, 2, 2);\r
102                 textGrid.addWidget(new QLabel(tr("Limit:")), 3,1);\r
103                 textGrid.addWidget(new QLabel(uploadLimit.toString() +" MB"),3,2);\r
104                 textGrid.addWidget(new QLabel(tr("Uploaded In This Period:")), 4,1);\r
105                 if (uploadAmt > 0)\r
106                         textGrid.addWidget(new QLabel(uploadAmt.toString()+unit +" ("+pct+"%)"),4,2);\r
107                 else\r
108                         textGrid.addWidget(new QLabel(tr("Less than 1MB")),4,2);\r
109                 textGrid.addWidget(new QLabel(tr("Current Cycle Ends:")), 5,1);\r
110                 textGrid.addWidget(new QLabel(endDate.toString()),5,2);\r
111                 limitGroup.setLayout(textGrid);\r
112 \r
113                 grid.addWidget(limitGroup, 1, 1);\r
114 \r
115                 UserAttributes attrib = Global.getUserAttributes();\r
116                 QGridLayout attribGrid = new QGridLayout();\r
117                 QGroupBox attribGroup = new QGroupBox(tr("User Attributes"));\r
118                 attribGrid.addWidget(new QLabel(tr("Incoming Email:")), 1,1);\r
119                 String server = Global.getServer();\r
120                 if (server.startsWith("www."))\r
121                         server = server.substring(4);\r
122                 \r
123                 //usually evernote mail is user@m.evernote.com\r
124                 server = "m."+server;\r
125                 \r
126                 attribGrid.addWidget(createIncomingEmailField(attrib.getIncomingEmailAddress()+"@"+server), 1,2);\r
127                 attribGroup.setLayout(attribGrid);\r
128                 grid.addWidget(attribGroup, 2,1);\r
129 \r
130                 \r
131                 QHBoxLayout buttonLayout = new QHBoxLayout();\r
132                 ok = new QPushButton("OK");\r
133                 ok.clicked.connect(this, "okPushed()");\r
134                 buttonLayout.addStretch();\r
135                 buttonLayout.addWidget(ok);\r
136                 buttonLayout.addStretch();\r
137                 grid.addLayout(buttonLayout,3,1);\r
138         }\r
139         \r
140         // build a field used for the incomming email\r
141         private QWidget createIncomingEmailField(String email){\r
142                 QTextEdit emailTextEdit = new QTextEdit();\r
143                 \r
144                 String emailLinkFormat="<a href=\"mailto:%1$s\">%1$s</a>";\r
145                 String emailHtml = String.format(emailLinkFormat, email);\r
146                 \r
147                 emailTextEdit.setLineWrapMode(LineWrapMode.NoWrap);\r
148 \r
149                 QTextDocument doc = new QTextDocument();\r
150                 doc.setHtml(emailHtml);\r
151                 doc.setMaximumBlockCount(1);\r
152                 \r
153                 emailTextEdit.setDocument(doc);\r
154                 emailTextEdit.setReadOnly(true);\r
155 \r
156                 //set background color as for disabled control \r
157                 QPalette palette = new QPalette();\r
158                 QColor backgroundColor = QApplication.palette().color(ColorRole.Window);\r
159                 palette.setColor(ColorRole.Base, backgroundColor);\r
160                 \r
161                 emailTextEdit.setAutoFillBackground(true);\r
162                 emailTextEdit.setPalette(palette);\r
163                 \r
164                 //remove frame borders\r
165                 emailTextEdit.setFrameShape(QFrame.Shape.NoFrame);\r
166                 \r
167                 //set height of emailTextEdit actually single line\r
168                 QFontMetrics fontMetrics = emailTextEdit.fontMetrics();\r
169                 //we also add some pixels to avoid showing scrollbars\r
170                 int height = fontMetrics.height() + emailTextEdit.frameWidth()*2 + 8;\r
171                 emailTextEdit.setFixedHeight(height);\r
172                 \r
173                 return emailTextEdit;\r
174         }\r
175         \r
176         // OK button pushed, close the window\r
177         @SuppressWarnings("unused")\r
178         private void okPushed() {\r
179                 this.close();\r
180         }\r
181         \r
182         // return the actual OK button\r
183         public QPushButton getOkButton() {\r
184                 return ok;\r
185         }\r
186 }\r