1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef MOZC_GUI_WORD_REGISTER_DIALOG_H_
31 #define MOZC_GUI_WORD_REGISTER_DIALOG_H_
32 
33 #include <QtCore/QString>
34 #include <QtGui/QtGui>
35 #include <QtWidgets/QDialog>
36 
37 #include <memory>
38 
39 #include "base/port.h"
40 #include "gui/word_register_dialog/ui_word_register_dialog.h"
41 
42 namespace mozc {
43 
44 class POSListProviderInterface;
45 
46 namespace client {
47 class ClientInterface;
48 }  // namespace client
49 
50 namespace user_dictionary {
51 class UserDictionarySession;
52 }  // namespace user_dictionary
53 
54 namespace gui {
55 class WordRegisterDialog : public QDialog,
56                            private Ui::WordRegisterDialog {
57   Q_OBJECT;
58 
59  public:
60   WordRegisterDialog();
61   virtual ~WordRegisterDialog();
62 
63   bool IsAvailable() const;
64 
65  protected slots:
66   void Clicked(QAbstractButton *button);
67   void LineEditChanged(const QString &str);
68   void CompleteReading();
69   void LaunchDictionaryTool();
70 
71  private:
72   enum ErrorCode {
73     SAVE_SUCCESS,
74     SAVE_FAILURE,
75     INVALID_KEY,
76     INVALID_VALUE,
77     EMPTY_KEY,
78     EMPTY_VALUE,
79     FATAL_ERROR
80   };
81 
82   ErrorCode SaveEntry();
83 
84   void UpdateUIStatus();
85 
86   // Copy the current selected text on the foreground window
87   // to clipboard. This method should be invoked before
88   // the word register form is activated.
89   // Seems that both ATOK and MS-IME use a clipboard to
90   // copy the selected text to the word register dialog.
91   // Clipboard seems to be the most robust mechanism to know
92   // the selected text. It works on almost all applications.
93   // TODO(all): Mac version is not available.
94   void CopyCurrentSelectionToClipboard();
95 
96   // Load text from clipboard.
97   void SetDefaultEntryFromClipboard();
98 
99   // Load text from environment variable.  Currently this method is
100   // tested only on Mac OSX and Windows.
101   // Return false if source environment variable is not found.
102   bool SetDefaultEntryFromEnvironmentVariable();
103 
104   // Return reading of value with reverse conversion feature.
105   const QString GetReading(const QString &value);
106 
107   // remove "\n" "\r" from |value|.
108   // remove whitespace from the start and the end.
109   const QString TrimValue(const QString &value) const;
110 
111   // turn on IME.
112   // When the dialog is shown, it is better to turn on IME.
113   void EnableIME();
114 
115   bool is_available_;
116   std::unique_ptr<mozc::user_dictionary::UserDictionarySession> session_;
117   std::unique_ptr<client::ClientInterface> client_;
118   QString window_title_;
119   std::unique_ptr<const POSListProviderInterface> pos_list_provider_;
120 };
121 
122 }  // namespace gui
123 }  // namespace mozc
124 
125 #endif  // MOZC_GUI_WORD_REGISTER_DIALOG_H_
126