OSDN Git Service

Move the directories
[kita/kita.git] / src / writetabwidget.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 ***************************************************************************/
10
11 #include "writetabwidget.h"
12
13 #include <QtGui/QClipboard>
14
15 #include <kaction.h>
16 #include <kactioncollection.h>
17 #include <kmessagebox.h>
18
19 #include "writeview.h"
20 #include "libkita/boarddatabase.h"
21 #include "libkita/datmanager.h"
22 #include "libkita/kita_misc.h"
23
24 using namespace Kita;
25
26 WriteTabWidget::WriteTabWidget(QWidget* parent) : TabWidgetBase(parent)
27 {
28     setXMLFile("writetabwidgetui.rc");
29
30     setupActions();
31 }
32
33
34 WriteTabWidget::~WriteTabWidget() {}
35
36
37 /* public slot */
38 void WriteTabWidget::slotShowWriteView(const KUrl& url,
39         const QString& resStr)
40 {
41     openWriteView(url, resStr, QString());
42 }
43
44
45 /* private */
46 void WriteTabWidget::openWriteView(const KUrl& url,
47                                         const QString& resStr, const QString& subject)
48 {
49     // TODO: machiBBS kakiko support.
50     BoardDatabase db(url);
51     if (db.type() == Board_MachiBBS) {
52 //        KMessageBox::sorry(this,
53 //                i18n("Can't write to machi BBS in this version."),
54 //                "<(_ _)>");
55 //        return ;
56     }
57
58     /* view exists */
59     WriteView* view = findWriteView(url);
60     if (view) {
61         if (view->body().length()) {
62             if (KMessageBox::warningYesNo(this,
63                     i18n("Do you want to clear the text?"), "Kita")
64                     == KMessageBox::No) return;
65         }
66
67         /* clear */
68         view->setMessage(resStr);
69         setCurrentWidget(view);
70         return ;
71     }
72
73     // TODO: refactoring.
74     /* create new write view & add it to tab */
75     QString threadName;
76
77     /* write res */
78     WriteView* new_dlg;
79     threadName = DatManager(url).threadName();
80     new_dlg = new WriteView(this, url);
81     new_dlg->setMessage(resStr);
82     addTab(new_dlg, threadName);
83     setCurrentWidget(new_dlg);
84 }
85
86 /* private */
87 WriteView* WriteTabWidget::findWriteView(const KUrl& url)
88 {
89     KUrl datUrl = getDatUrl(url);
90     if (datUrl.isEmpty()) return 0;
91
92     int max = count();
93     if (max == 0) return 0;
94
95     for(int i=0; i < max; i++) {
96         WriteView * view = isWriteView(widget (i));
97         if (view) {
98             if (view->datUrl() == datUrl) return view;
99         }
100     }
101
102     return 0;
103 }
104
105
106 /* private */
107 WriteView* WriteTabWidget::isWriteView(QWidget* w)
108 {
109     return qobject_cast<WriteView*>(w);
110 }
111
112
113
114 /* when thread view is focused, this slot is called    */
115 /* See also ThreadView::setFocus.                  */ /* private slot */
116 void WriteTabWidget::slotChangeWriteTab(const KUrl& url)
117 {
118     WriteView * view;
119     int max = count();
120     if (max == 0) return ;
121
122     /* disable all ok buttons. */
123     for(int i=0; i < max ; i++) {
124         view = isWriteView(widget(i));
125         if (view) view->slotEnableWriting(false);
126     }
127
128     /* show current url page. */
129     view = findWriteView(url);
130     if (view) {
131         if (currentWidget() != view) setCurrentWidget(view);
132         view->slotEnableWriting(true);
133     }
134 }
135
136
137 /* protected */ /* virtual */
138 void WriteTabWidget::deleteWidget(QWidget* w)
139 {
140     WriteView * view = isWriteView(w);
141
142     if (view == 0) return ;
143
144     if (view->body().length()) {
145         if (KMessageBox::warningYesNo(this,
146                 i18n("If you close this dialog, you will lose text.\n"
147                 "Do you want to close?"), "Kita") == KMessageBox::No) return;
148     }
149
150     TabWidgetBase::deleteWidget(w);
151 }
152
153
154 /*--------------------------------*/
155 /* WriteTabWidget actions */
156
157
158 /* private */
159 void WriteTabWidget::setupActions()
160 {
161     KAction* quoteclip_action = actionCollection()->addAction("writeview_quoteclip");
162     quoteclip_action->setText(i18n("quote clipboard"));
163     quoteclip_action->setShortcut(Qt::Key_F2);
164     connect(quoteclip_action, SIGNAL(triggered()), this, SLOT(slotQuoteClipboard()));
165 }
166
167
168 /* public slot */
169 void WriteTabWidget::slotQuoteClipboard()
170 {
171     WriteView * view = isWriteView(currentWidget());
172     if (view) {
173         QClipboard * clipboard = QApplication::clipboard();
174         QString str = clipboard->text(QClipboard::Selection);
175         if (str.isEmpty()) str = clipboard->text(QClipboard::Clipboard);
176         if (!str.isEmpty()) {
177
178             QString msg = "\n> " + str.replace('\n', "\n> ") + '\n';
179             view->insertMessage(msg);
180         }
181     }
182 }