kalarm/lib

lineedit.cpp
1/*
2 * lineedit.cpp - Line edit widget with extra drag and drop options
3 * Program: kalarm
4 * Copyright (C) 2003 - 2005 by David Jarvie <software@astrojar.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include "kalarm.h"
22
23#include <tqregexp.h>
24#include <tqdragobject.h>
25
26#include <kurldrag.h>
27#include <kurlcompletion.h>
28
29#include <libtdepim/maillistdrag.h>
30#include <libtdepim/kvcarddrag.h>
31#include <libkcal/icaldrag.h>
32
33#include "lineedit.moc"
34
35
36/*=============================================================================
37= Class LineEdit
38= Line edit which accepts drag and drop of text, URLs and/or email addresses.
39* It has an option to prevent its contents being selected when it receives
40= focus.
41=============================================================================*/
42LineEdit::LineEdit(Type type, TQWidget* parent, const char* name)
43 : KLineEdit(parent, name),
44 mType(type),
45 mNoSelect(false),
46 mSetCursorAtEnd(false)
47{
48 init();
49}
50
51LineEdit::LineEdit(TQWidget* parent, const char* name)
52 : KLineEdit(parent, name),
53 mType(Text),
54 mNoSelect(false),
55 mSetCursorAtEnd(false)
56{
57 init();
58}
59
60void LineEdit::init()
61{
62 if (mType == Url)
63 {
64 setCompletionMode(TDEGlobalSettings::CompletionShell);
65 KURLCompletion* comp = new KURLCompletion(KURLCompletion::FileCompletion);
66 comp->setReplaceHome(true);
67 setCompletionObject(comp);
68 setAutoDeleteCompletionObject(true);
69 }
70 else
71 setCompletionMode(TDEGlobalSettings::CompletionNone);
72}
73
74/******************************************************************************
75* Called when the line edit receives focus.
76* If 'noSelect' is true, prevent the contents being selected.
77*/
78void LineEdit::focusInEvent(TQFocusEvent* e)
79{
80 if (mNoSelect)
81 e->setReason(TQFocusEvent::Other);
82 KLineEdit::focusInEvent(e);
83 if (mNoSelect)
84 {
85 e->resetReason();
86 mNoSelect = false;
87 }
88}
89
90void LineEdit::setText(const TQString& text)
91{
92 KLineEdit::setText(text);
93 setCursorPosition(mSetCursorAtEnd ? text.length() : 0);
94}
95
96void LineEdit::dragEnterEvent(TQDragEnterEvent* e)
97{
98 if (KCal::ICalDrag::canDecode(e))
99 e->accept(false); // don't accept "text/calendar" objects
100 e->accept(TQTextDrag::canDecode(e)
101 || KURLDrag::canDecode(e)
102 || (mType != Url && KPIM::MailListDrag::canDecode(e))
103 || (mType == Emails && KVCardDrag::canDecode(e)));
104}
105
106void LineEdit::dropEvent(TQDropEvent* e)
107{
108 TQString newText;
109 TQStringList newEmails;
110 TQString txt;
111 KPIM::MailList mailList;
112 KURL::List files;
113 TDEABC::Addressee::List addrList;
114
115 if (mType != Url
116 && e->provides(KPIM::MailListDrag::format())
117 && KPIM::MailListDrag::decode(e, mailList))
118 {
119 // KMail message(s) - ignore all but the first
120 if (mailList.count())
121 {
122 if (mType == Emails)
123 newText = mailList.first().from();
124 else
125 setText(mailList.first().subject()); // replace any existing text
126 }
127 }
128 // This must come before KURLDrag
129 else if (mType == Emails
130 && KVCardDrag::canDecode(e) && KVCardDrag::decode(e, addrList))
131 {
132 // KAddressBook entries
133 for (TDEABC::Addressee::List::Iterator it = addrList.begin(); it != addrList.end(); ++it)
134 {
135 TQString em((*it).fullEmail());
136 if (!em.isEmpty())
137 newEmails.append(em);
138 }
139 }
140 else if (KURLDrag::decode(e, files) && files.count())
141 {
142 // URL(s)
143 switch (mType)
144 {
145 case Url:
146 // URL entry field - ignore all but the first dropped URL
147 setText(files.first().prettyURL()); // replace any existing text
148 break;
149 case Emails:
150 {
151 // Email entry field - ignore all but mailto: URLs
152 TQString mailto = TQString::fromLatin1("mailto");
153 for (KURL::List::Iterator it = files.begin(); it != files.end(); ++it)
154 {
155 if ((*it).protocol() == mailto)
156 newEmails.append((*it).path());
157 }
158 break;
159 }
160 case Text:
161 newText = files.first().prettyURL();
162 break;
163 }
164 }
165 else if (TQTextDrag::decode(e, txt))
166 {
167 // Plain text
168 if (mType == Emails)
169 {
170 // Remove newlines from a list of email addresses, and allow an eventual mailto: protocol
171 TQString mailto = TQString::fromLatin1("mailto:");
172 newEmails = TQStringList::split(TQRegExp("[\r\n]+"), txt);
173 for (TQStringList::Iterator it = newEmails.begin(); it != newEmails.end(); ++it)
174 {
175 if ((*it).startsWith(mailto))
176 {
177 KURL url(*it);
178 *it = url.path();
179 }
180 }
181 }
182 else
183 {
184 int newline = txt.find('\n');
185 newText = (newline >= 0) ? txt.left(newline) : txt;
186 }
187 }
188
189 if (newEmails.count())
190 {
191 newText = newEmails.join(",");
192 int c = cursorPosition();
193 if (c > 0)
194 newText.prepend(",");
195 if (c < static_cast<int>(text().length()))
196 newText.append(",");
197 }
198 if (!newText.isEmpty())
199 insert(newText);
200}
static bool canDecode(TQMimeSource *e)
static bool decode(TQMimeSource *e, TQString &content)
Type
Types of drag and drop content which will be accepted.
Definition: lineedit.h:62
LineEdit(Type type, TQWidget *parent=0, const char *name=0)
Constructor.
Definition: lineedit.cpp:42
virtual void setText(const TQString &str)
Sets the contents of the line edit to be str.
Definition: lineedit.cpp:90