TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lineedit.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include "kalarm.h"
  21. #include <tqregexp.h>
  22. #include <tqdragobject.h>
  23. #include <kurldrag.h>
  24. #include <kurlcompletion.h>
  25. #include <libtdepim/maillistdrag.h>
  26. #include <libtdepim/kvcarddrag.h>
  27. #include <libkcal/icaldrag.h>
  28. #include "lineedit.moc"
  29. /*=============================================================================
  30. = Class LineEdit
  31. = Line edit which accepts drag and drop of text, URLs and/or email addresses.
  32. * It has an option to prevent its contents being selected when it receives
  33. = focus.
  34. =============================================================================*/
  35. LineEdit::LineEdit(Type type, TQWidget* parent, const char* name)
  36. : KLineEdit(parent, name),
  37. mType(type),
  38. mNoSelect(false),
  39. mSetCursorAtEnd(false)
  40. {
  41. init();
  42. }
  43. LineEdit::LineEdit(TQWidget* parent, const char* name)
  44. : KLineEdit(parent, name),
  45. mType(Text),
  46. mNoSelect(false),
  47. mSetCursorAtEnd(false)
  48. {
  49. init();
  50. }
  51. void LineEdit::init()
  52. {
  53. if (mType == Url)
  54. {
  55. setCompletionMode(KGlobalSettings::CompletionShell);
  56. KURLCompletion* comp = new KURLCompletion(KURLCompletion::FileCompletion);
  57. comp->setReplaceHome(true);
  58. setCompletionObject(comp);
  59. setAutoDeleteCompletionObject(true);
  60. }
  61. else
  62. setCompletionMode(KGlobalSettings::CompletionNone);
  63. }
  64. /******************************************************************************
  65. * Called when the line edit receives focus.
  66. * If 'noSelect' is true, prevent the contents being selected.
  67. */
  68. void LineEdit::focusInEvent(TQFocusEvent* e)
  69. {
  70. if (mNoSelect)
  71. e->setReason(TQFocusEvent::Other);
  72. KLineEdit::focusInEvent(e);
  73. if (mNoSelect)
  74. {
  75. e->resetReason();
  76. mNoSelect = false;
  77. }
  78. }
  79. void LineEdit::setText(const TQString& text)
  80. {
  81. KLineEdit::setText(text);
  82. setCursorPosition(mSetCursorAtEnd ? text.length() : 0);
  83. }
  84. void LineEdit::dragEnterEvent(TQDragEnterEvent* e)
  85. {
  86. if (KCal::ICalDrag::canDecode(e))
  87. e->accept(false); // don't accept "text/calendar" objects
  88. e->accept(TQTextDrag::canDecode(e)
  89. || KURLDrag::canDecode(e)
  90. || mType != Url && KPIM::MailListDrag::canDecode(e)
  91. || mType == Emails && KVCardDrag::canDecode(e));
  92. }
  93. void LineEdit::dropEvent(TQDropEvent* e)
  94. {
  95. TQString newText;
  96. TQStringList newEmails;
  97. TQString txt;
  98. KPIM::MailList mailList;
  99. KURL::List files;
  100. KABC::Addressee::List addrList;
  101. if (mType != Url
  102. && e->provides(KPIM::MailListDrag::format())
  103. && KPIM::MailListDrag::decode(e, mailList))
  104. {
  105. // KMail message(s) - ignore all but the first
  106. if (mailList.count())
  107. {
  108. if (mType == Emails)
  109. newText = mailList.first().from();
  110. else
  111. setText(mailList.first().subject()); // replace any existing text
  112. }
  113. }
  114. // This must come before KURLDrag
  115. else if (mType == Emails
  116. && KVCardDrag::canDecode(e) && KVCardDrag::decode(e, addrList))
  117. {
  118. // KAddressBook entries
  119. for (KABC::Addressee::List::Iterator it = addrList.begin(); it != addrList.end(); ++it)
  120. {
  121. TQString em((*it).fullEmail());
  122. if (!em.isEmpty())
  123. newEmails.append(em);
  124. }
  125. }
  126. else if (KURLDrag::decode(e, files) && files.count())
  127. {
  128. // URL(s)
  129. switch (mType)
  130. {
  131. case Url:
  132. // URL entry field - ignore all but the first dropped URL
  133. setText(files.first().prettyURL()); // replace any existing text
  134. break;
  135. case Emails:
  136. {
  137. // Email entry field - ignore all but mailto: URLs
  138. TQString mailto = TQString::tqfromLatin1("mailto");
  139. for (KURL::List::Iterator it = files.begin(); it != files.end(); ++it)
  140. {
  141. if ((*it).protocol() == mailto)
  142. newEmails.append((*it).path());
  143. }
  144. break;
  145. }
  146. case Text:
  147. newText = files.first().prettyURL();
  148. break;
  149. }
  150. }
  151. else if (TQTextDrag::decode(e, txt))
  152. {
  153. // Plain text
  154. if (mType == Emails)
  155. {
  156. // Remove newlines from a list of email addresses, and allow an eventual mailto: protocol
  157. TQString mailto = TQString::tqfromLatin1("mailto:");
  158. newEmails = TQStringList::split(TQRegExp("[\r\n]+"), txt);
  159. for (TQStringList::Iterator it = newEmails.begin(); it != newEmails.end(); ++it)
  160. {
  161. if ((*it).startsWith(mailto))
  162. {
  163. KURL url(*it);
  164. *it = url.path();
  165. }
  166. }
  167. }
  168. else
  169. {
  170. int newline = txt.find('\n');
  171. newText = (newline >= 0) ? txt.left(newline) : txt;
  172. }
  173. }
  174. if (newEmails.count())
  175. {
  176. newText = newEmails.join(",");
  177. int c = cursorPosition();
  178. if (c > 0)
  179. newText.prepend(",");
  180. if (c < static_cast<int>(text().length()))
  181. newText.append(",");
  182. }
  183. if (!newText.isEmpty())
  184. insert(newText);
  185. }