1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4 
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #pragma once
9 
10 #include "ui/widgets/input_fields.h"
11 #include "base/timer.h"
12 #include "base/qt_connection.h"
13 
14 #ifndef TDESKTOP_DISABLE_SPELLCHECK
15 #include "boxes/dictionaries_manager.h"
16 #include "spellcheck/spelling_highlighter.h"
17 #endif // TDESKTOP_DISABLE_SPELLCHECK
18 
19 #include <QtGui/QClipboard>
20 
21 namespace Main {
22 class Session;
23 } // namespace Main
24 
25 namespace Window {
26 class SessionController;
27 } // namespace Window
28 
29 namespace Ui {
30 class PopupMenu;
31 } // namespace Ui
32 
33 QString PrepareMentionTag(not_null<UserData*> user);
34 TextWithTags PrepareEditText(not_null<HistoryItem*> item);
35 
36 Fn<bool(
37 	Ui::InputField::EditLinkSelection selection,
38 	QString text,
39 	QString link,
40 	Ui::InputField::EditLinkAction action)> DefaultEditLinkCallback(
41 		not_null<Window::SessionController*> controller,
42 		not_null<Ui::InputField*> field);
43 void InitMessageField(
44 	not_null<Window::SessionController*> controller,
45 	not_null<Ui::InputField*> field);
46 
47 void InitSpellchecker(
48 	not_null<Window::SessionController*> controller,
49 	not_null<Ui::InputField*> field);
50 
51 bool HasSendText(not_null<const Ui::InputField*> field);
52 
53 struct InlineBotQuery {
54 	QString query;
55 	QString username;
56 	UserData *bot = nullptr;
57 	bool lookingUpBot = false;
58 };
59 InlineBotQuery ParseInlineBotQuery(
60 	not_null<Main::Session*> session,
61 	not_null<const Ui::InputField*> field);
62 
63 struct AutocompleteQuery {
64 	QString query;
65 	bool fromStart = false;
66 };
67 AutocompleteQuery ParseMentionHashtagBotCommandQuery(
68 	not_null<const Ui::InputField*> field);
69 
70 class MessageLinksParser : private QObject {
71 public:
72 	MessageLinksParser(not_null<Ui::InputField*> field);
73 
74 	void parseNow();
75 
76 	[[nodiscard]] const rpl::variable<QStringList> &list() const;
77 
78 protected:
79 	bool eventFilter(QObject *object, QEvent *event) override;
80 
81 private:
82 	struct LinkRange {
83 		int start;
84 		int length;
85 		QString custom;
86 	};
87 	friend inline bool operator==(const LinkRange &a, const LinkRange &b) {
88 		return (a.start == b.start)
89 			&& (a.length == b.length)
90 			&& (a.custom == b.custom);
91 	}
92 	friend inline bool operator!=(const LinkRange &a, const LinkRange &b) {
93 		return !(a == b);
94 	}
95 
96 	void parse();
97 	void apply(const QString &text, const QVector<LinkRange> &ranges);
98 
99 	not_null<Ui::InputField*> _field;
100 	rpl::variable<QStringList> _list;
101 	int _lastLength = 0;
102 	base::Timer _timer;
103 	base::qt_connection _connection;
104 
105 };
106