summaryrefslogtreecommitdiffstats
path: root/src/PYPCloudCandidates.h
blob: ab7494da662ba97fa99c9b5501074b7186a5a85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* vim:set et ts=4 sts=4:
 *
 * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
 *
 * Copyright (c) 2018 linyu Xu <liannaxu07@gmail.com>
 * Copyright (c) 2020 Weixuan XIAO <veyx.shaw@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef __PY_LIB_PINYIN_ClOUD_CANDIDATES_H_
#define __PY_LIB_PINYIN_ClOUD_CANDIDATES_H_

#include "PYString.h"
#include "PYPointer.h"
#include "PYPEnhancedCandidates.h"
#include <string>
#include <vector>
#include <set>
#include <glib.h>
#include <libsoup/soup.h>
#include <json-glib/json-glib.h>
#include "PYConfig.h"



class CloudCandidatesResponseParser;

namespace PY {

#define BUFFERLENGTH 2048
#define CLOUD_MINIMUM_UTF8_TRIGGER_LENGTH 2

enum InputMode {
    FullPinyin = 0,
    DoublePinyin,
    Bopomofo
};

class PhoneticEditor;

class CloudCandidates : public EnhancedCandidates<PhoneticEditor>
{
public:

    CloudCandidates (PhoneticEditor *editor);
    ~CloudCandidates();

    void setInputMode (InputMode mode) { m_input_mode = mode; }

    gboolean processCandidates (std::vector<EnhancedCandidate> & candidates);

    int selectCandidate (EnhancedCandidate & enhanced);

    void cloudAsyncRequest (gpointer user_data);
    void cloudSyncRequest (const gchar* pinyin, std::vector<EnhancedCandidate> & candidates);

    void delayedCloudAsyncRequest (const gchar* pinyin);

    void updateLookupTable ();

    guint m_source_event_id;
    SoupMessage *m_message;
    GCancellable *m_cancel_message;
    std::string m_last_requested_pinyin;

private:
    static gboolean delayedCloudAsyncRequestCallBack (gpointer user_data);
    static void cloudResponseCallBack (GObject *object, GAsyncResult *result, gpointer user_data);

    gboolean processCloudResponse (GInputStream *stream, std::vector<EnhancedCandidate> & candidates, const gchar *pinyin);

    /* get internal full pinyin representation */
    String getFullPinyin ();

    void resetCloudResponseParser ();

private:
    SoupSession *m_session;
    InputMode m_input_mode;

    CloudInputSource m_input_source;
    CloudCandidatesResponseParser *m_parser;
    GTimer *m_timer;

protected:
    std::vector<EnhancedCandidate> m_candidates;

    /* The candidate cache contains some candidates from libpinyin,
       use this cache to remove duplicated cloud candidates. */
    std::set<std::string> m_candidate_cache;
};

};

#endif