Changeset 558:30b0989e5b4e in qutecom-2.2


Ignore:
Timestamp:
Mar 8, 2010 12:24:29 PM (3 years ago)
Author:
laurent@…
Branch:
default
Message:

add google translation feature

Location:
wengophone
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • wengophone/resources/config/config.xml

    r488 r558  
    272272<feature.sms><bool>1</bool></feature.sms> 
    273273<feature.audioconference><bool>1</bool></feature.audioconference> 
    274  
     274<translation.sent><string>unknow</string></translation.sent> 
     275<translation.received><string>unknow</string></translation.received> 
    275276</settings> 
  • wengophone/src/model/config/Config.cpp

    r522 r558  
    209209const string Config::SIP_USE_TYPING_STATE="sip.use.typing_state"; 
    210210 
     211const string Config::TRANSLATION_SENT_KEY="translation.sent"; 
     212const string Config::TRANSLATION_RECEIVED_KEY="translation.received"; 
     213 
    211214string Config::_configDir; 
    212215 
     
    819822        return getBooleanKeyValue(SIP_USE_TYPING_STATE); 
    820823} 
     824 
     825string Config::getTranslationSent() const { 
     826        return getStringKeyValue(TRANSLATION_SENT_KEY); 
     827} 
     828 
     829string Config::getTranslationReceived() const { 
     830        return getStringKeyValue(TRANSLATION_RECEIVED_KEY); 
     831} 
  • wengophone/src/model/config/Config.h

    r522 r558  
    656656        /** @} */ 
    657657 
     658        /** 
     659         * @name translation Methods 
     660         * @{ 
     661         */ 
     662         
     663        static const std::string TRANSLATION_SENT_KEY; 
     664        std::string getTranslationSent() const; 
     665         
     666        static const std::string TRANSLATION_RECEIVED_KEY; 
     667        std::string getTranslationReceived() const; 
     668         
    658669private: 
    659670 
  • wengophone/src/presentation/qt/chat/QtChatHistoryWidget.cpp

    r516 r558  
    6565        page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks); 
    6666        connect(this,SIGNAL(linkClicked(const QUrl &)),this,SLOT(urlClicked(const QUrl &))); 
     67         
     68        //load(QUrl("/Users/laurent/Desktop/qutecom-2.2/debug/debug/QuteCom.app/Contents/Resources/chat/base.html")); 
    6769} 
    6870 
     
    7375void QtChatHistoryWidget::setTheme(const QString& themeDir) { 
    7476        _theme->load(themeDir); 
     77        Config & config = ConfigManager::getInstance().getCurrentConfig(); 
    7578         
    7679        QFile file(_theme->getCurrentThemePath()+"/main.css"); 
     
    7881        QUrl url_css("data:text/css;charset=utf-8;base64,"+file.readAll().toBase64()); 
    7982        page()->settings()->setUserStyleSheetUrl(url_css); 
     83         
     84        page()->mainFrame()->addToJavaScriptWindowObject("parent",this); 
     85        QFile file_base(QString::fromStdString(config.getResourcesDir())+"chat/base.html"); 
     86        file_base.open(QIODevice::ReadOnly); 
     87#if defined(OS_WINDOWS) 
     88        setHtml(file_base.readAll(),QUrl(_theme->getCurrentThemePath())); 
     89#else 
     90        setHtml(file_base.readAll(),QUrl("file://"+_theme->getCurrentThemePath())); 
     91#endif 
     92         
    8093} 
    8194 
     
    113126 
    114127void QtChatHistoryWidget::insertMessage(const QString & contactId, const QString & senderName, const QString & message, const QTime & time) { 
    115         //QTextCursor curs(document()); 
    116         //curs.movePosition(QTextCursor::End); 
    117         //setTextCursor(curs); 
    118  
     128         
    119129        // Prepare message 
    120130        QString htmlMessage = QtChatUtils::decodeMessage(_protocol, message); 
     131        Config & config = ConfigManager::getInstance().getCurrentConfig(); 
     132         
     133        page ()->currentFrame ()->evaluateJavaScript("setLanguage(\""+QString::fromStdString(config.getTranslationReceived())+"\")"); 
    121134 
    122135        // Get html 
     
    131144                if (!_lastSenderName.isEmpty())  
    132145                { 
    133                         _data +=  _theme->getFooter(); 
     146                        QString footer =  _theme->getFooter(); 
     147                        footer = footer.replace("\n",""); 
     148                        footer = footer.replace("\r",""); 
     149                        footer = footer.replace("\t",""); 
     150                        footer = footer.replace("\"","\\\""); 
     151                        footer = footer.replace("\'","\\\'"); 
     152                        page ()->currentFrame ()->evaluateJavaScript("insertMessageAtEnd(\""+footer+"\")"); 
    134153                } 
    135154                position = QtChatTheme::First; 
     
    149168        QtChatTheme::setKeywordValue(html, "sender", senderName); 
    150169        QtChatTheme::setKeywordValue(html, "time", time.toString("HH:mm")); 
    151         QtChatTheme::setKeywordValue(html, "message", htmlMessage); 
     170        QtChatTheme::setKeywordValue(html, "message", "<div id='magic_id'>"+htmlMessage+"</div>"); 
    152171        QtChatTheme::setKeywordValue(html, "service", ""); 
    153172 
     
    156175                avatar_path = _avatarMap[contactId]; 
    157176        QtChatTheme::setKeywordValue(html, "userIconPath", avatar_path); 
    158          
    159         QString theme_insert1 = "<span id=\"insert\"></span>"; 
    160         QString theme_insert2 = "<div id=\"insert\"></div>"; 
    161          
    162         if(position == QtChatTheme::Next && html.contains(theme_insert1)) 
    163         {        
    164                 _data.insert(_data.lastIndexOf(theme_insert1),html); 
    165         } 
    166         else if (position == QtChatTheme::Next && html.contains(theme_insert2)) 
    167         { 
    168                 _data.insert(_data.lastIndexOf(theme_insert2),html); 
    169         } 
     177 
     178        //updateHtml(); 
     179 
     180        html = html.replace("\n",""); 
     181        html = html.replace("\r",""); 
     182        html = html.replace("\t",""); 
     183        html = html.replace("\"","\\\""); 
     184        html = html.replace("\'","\\\'"); 
     185        if(position == QtChatTheme::Next) 
     186                page ()->currentFrame ()->evaluateJavaScript("insertMessage(\""+html+"\")"); 
    170187        else 
    171                 _data += html; 
    172  
    173         updateHtml(); 
     188                page ()->currentFrame ()->evaluateJavaScript("insertMessageAtEnd(\""+html+"\")"); 
     189         
     190        //scrollToBottom(); 
    174191} 
    175192 
     
    177194        // Make sure we close any opened message group 
    178195        if (!_lastSenderName.isEmpty()) { 
    179                 _data += _theme->getFooter(); 
     196                QString footer = _theme->getFooter(); 
     197                page ()->currentFrame ()->evaluateJavaScript("insertMessageAtEnd(\""+footer+"\")"); 
    180198                _lastSenderName = ""; 
    181199        } 
     
    184202        QtChatTheme::setKeywordValue(html, "time", time.toString("HH:mm")); 
    185203         
    186         Config & config = ConfigManager::getInstance().getCurrentConfig(); 
    187         QString themeDir = QString::fromStdString(config.getChatTheme()); 
    188          
    189         _data += html; 
    190  
    191         updateHtml();    
     204        html = html.replace("\n",""); 
     205        html = html.replace("\r",""); 
     206        html = html.replace("\t",""); 
     207        html = html.replace("\"","\\\""); 
     208        html = html.replace("\'","\\\'"); 
     209        page ()->currentFrame ()->evaluateJavaScript("insertMessageAtEnd(\""+html+"\")"); 
     210         
     211        //_data += html; 
     212         
    192213} 
    193214 
    194215void QtChatHistoryWidget::setAvatarPixmap(const QString& name, const QPixmap& pixmap)  
    195216{ 
    196         QString path = QDir::temp().absolutePath()+QDir::separator()+name+".png"; 
     217        QDir dir(QString::fromStdString(Config::getConfigDir())); 
     218         
     219        if(!dir.exists("cache")) 
     220                dir.mkdir("cache"); 
     221        dir.cd("cache"); 
     222         
     223        QString path = dir.absolutePath()+QDir::separator()+name+".png"; 
     224 
    197225        pixmap.save(path,"PNG"); 
    198         _avatarMap[name] = path;         
    199 } 
    200  
    201 /*QVariant QtChatHistoryWidget::loadResource(int type, const QUrl& url) { 
    202         if (type != QTextDocument::ImageResource || url.scheme() != "avatar") { 
    203                 return QTextBrowser::loadResource(type, url); 
    204         } 
    205  
    206         QString userId = url.path(); 
    207         if (!_avatarMap.contains(userId)) { 
    208                 std::string data = AvatarList::getInstance().getDefaultAvatarPicture().getData(); 
    209                 QPixmap pixmap; 
    210                 pixmap.loadFromData((uchar *)data.c_str(), data.size()); 
    211                 _avatarMap[userId] = pixmap; 
    212         } 
    213         QPixmap pixmap = _avatarMap[userId]; 
    214  
    215         if (!pixmap.isNull()) { 
    216                 QString sizeString = url.queryItemValue("size"); 
    217                 bool ok; 
    218                 int size = sizeString.toInt(&ok); 
    219                 if (ok) { 
    220                         if (_scaledAvatarCache.contains(url)) { 
    221                                 pixmap = _scaledAvatarCache[url]; 
    222                         } else { 
    223                                 pixmap = pixmap.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation); 
    224                                 _scaledAvatarCache[url] = pixmap; 
    225                         } 
    226                 } 
    227         } 
    228  
    229         return QVariant(pixmap); 
    230 }*/ 
     226        _avatarMap[name] = "file://"+path;       
     227} 
    231228 
    232229void QtChatHistoryWidget::scrollToBottom() { 
    233         // We do not use ensureCursorVisible() because it cause the view to get 
    234         // scrolled horizontally if the last line does not fit the view. 
    235         //verticalScrollBar()->setValue(verticalScrollBar()->maximum()); 
     230         
    236231        QWebFrame *     frame = page()->mainFrame (); 
    237232        frame->setScrollBarValue(Qt::Vertical,frame->scrollBarMaximum(Qt::Vertical)); 
     
    244239} 
    245240 
    246 void QtChatHistoryWidget::slot_loadFinished(bool) 
    247 { 
    248         scrollToBottom(); 
    249 } 
    250241 
    251242void QtChatHistoryWidget::updateHtml() 
  • wengophone/src/presentation/qt/chat/QtChatHistoryWidget.h

    r503 r558  
    7575         */ 
    7676        void urlClicked(const QUrl & link); 
     77         
     78public Q_SLOTS: 
     79        void scrollToBottom(); 
    7780 
    78         void scrollToBottom(); 
    79          
    80         void slot_loadFinished(bool); 
    81          
    8281private: 
    8382        void updateHtml(); 
  • wengophone/src/presentation/qt/chat/QtChatWidget.cpp

    r503 r558  
    6565static const int EMOTICON_OFFSET_Y = 30; 
    6666 
     67QString clearHtml( const QString & str) 
     68{ 
     69        QString html = str; 
     70         
     71        QRegExp htmlDocumentToHtmlSnippet(".*<body[^>]*>\\s*<p[^>]*>(.*)</p>\\s*</body>\\s*</html>"); 
     72        QRegExp pTag("<p[^>]*>"); 
     73 
     74        // toHtml() returns an HTML document, complete with html and body tags. 
     75        // This regexp strip those to produce an HTML snippet, which can be 
     76        // concatenated to the existing history 
     77        html.replace(htmlDocumentToHtmlSnippet, "\\1"); 
     78        html.replace(pTag, "<div>"); 
     79        html.replace("</p>", "</div>"); 
     80         
     81        return html; 
     82} 
     83 
    6784QtChatWidget::QtChatWidget(CChatHandler & cChatHandler, 
    6885        QtWengoPhone * qtWengoPhone, int sessionId, 
     
    136153        SAFE_CONNECT_TYPE(this, SIGNAL(profileChangedEventHandlerSignal()),  
    137154                SLOT(updateUserAvatar()), Qt::QueuedConnection); 
     155                 
     156        _webView = new QWebView; 
     157        _webView->page()->mainFrame()->addToJavaScriptWindowObject("parent",this); 
     158        Config & config = ConfigManager::getInstance().getCurrentConfig(); 
     159        QFile file(QString::fromStdString(config.getResourcesDir())+"chat/translate.html"); 
     160        file.open(QIODevice::ReadOnly); 
     161        _webView->setHtml(file.readAll());       
    138162} 
    139163 
     
    142166         
    143167        _stoppedTypingTimer->stop(); 
     168         
     169        delete _webView; 
    144170} 
    145171 
     
    185211        _underline = !_underline; 
    186212        _ui.chatEdit->setFontUnderline(_underline); 
     213} 
     214 
     215void QtChatWidget::translateClickedSlot() 
     216{ 
     217        Config & config = ConfigManager::getInstance().getCurrentConfig();       
     218        QString lang = QString::fromStdString(config.getTranslationSent());      
     219        QString text = clearHtml(_ui.chatEdit->toHtml()); 
     220         
     221        if(text.isEmpty() || lang == "unknow") 
     222                return; 
     223         
     224        text = text.replace("\"","\\\""); 
     225        text = text.replace("\'","\\\'"); 
     226         
     227        _webView->page ()->mainFrame ()->evaluateJavaScript("setLanguage(\""+QString::fromStdString(config.getTranslationSent())+"\")"); 
     228        _webView->page ()->mainFrame ()->evaluateJavaScript("translate(\""+text+"\")"); 
    187229} 
    188230 
     
    292334} 
    293335 
    294 void QtChatWidget::sendMessage() { 
    295         QRegExp htmlDocumentToHtmlSnippet(".*<body[^>]*>\\s*<p[^>]*>(.*)</p>\\s*</body>\\s*</html>"); 
    296         QRegExp pTag("<p[^>]*>"); 
    297  
     336void QtChatWidget::sendMessage()  
     337{ 
     338        Config & config = ConfigManager::getInstance().getCurrentConfig(); 
     339        QString lang = QString::fromStdString(config.getTranslationSent()); 
     340         
     341        if(lang=="unknow")       
     342           sendMessage(_ui.chatEdit->toHtml()); 
     343        else 
     344           translateClickedSlot(); 
     345} 
     346 
     347void QtChatWidget::sendMessage(const QString & text) { 
     348 
     349        QString html = clearHtml(text); 
    298350        //Drop empty message 
    299         if (_ui.chatEdit->toPlainText().isEmpty()) { 
     351        if (html.isEmpty()) { 
    300352                return; 
    301353        } 
    302  
    303         QString html = _ui.chatEdit->toHtml(); 
    304         // toHtml() returns an HTML document, complete with html and body tags. 
    305         // This regexp strip those to produce an HTML snippet, which can be 
    306         // concatenated to the existing history 
    307         html.replace(htmlDocumentToHtmlSnippet, "\\1"); 
    308         html.replace(pTag, "<div>"); 
    309         html.replace("</p>", "</div>"); 
    310354 
    311355        addToHistory("self", _nickName, html); 
     
    372416        _imChatSession->changeTypingState(IMChat::TypingStateStopTyping); 
    373417        _isTyping = false; 
     418} 
     419 
     420void QtChatWidget::translateFinishedSlot(const QVariant & var) 
     421{ 
     422        QString str = var.toString(); 
     423        str = str.replace("&#39;","'"); 
     424        sendMessage(str); 
    374425} 
    375426 
  • wengophone/src/presentation/qt/chat/QtChatWidget.h

    r0 r558  
    3434 
    3535#include <QtCore/QTime> 
     36#include <QtWebKit/QWebView> 
    3637 
    3738class Profile; 
     
    4344class QTimer; 
    4445 
     46#include <QtNetwork/QtNetwork> 
     47 
    4548/** 
    4649 * 
     
    137140 
    138141        void sendFileToSession(const QString & filename); 
     142         
     143        void translateFinishedSlot(const QVariant & ); 
    139144 
    140145Q_SIGNALS: 
     
    172177 
    173178        void updateUserAvatar(); 
     179         
     180        void translateClickedSlot(); 
    174181 
    175182private: 
     
    185192 
    186193        bool editKeyPressEventFilter(QKeyEvent* event); 
     194         
     195        void sendMessage(const QString & html); 
    187196 
    188197        CChatHandler & _cChatHandler; 
     
    221230         
    222231        bool _isTyping; 
     232 
     233        QWebView * _webView; 
    223234}; 
    224235 
  • wengophone/src/presentation/qt/config/LanguagesSettings.ui

    r0 r558  
    1 <ui version="4.0" > 
     1<?xml version="1.0" encoding="UTF-8"?> 
     2<ui version="4.0"> 
    23 <class>LanguagesSettings</class> 
    3  <widget class="QWidget" name="LanguagesSettings" > 
    4   <property name="geometry" > 
     4 <widget class="QWidget" name="LanguagesSettings"> 
     5  <property name="geometry"> 
    56   <rect> 
    67    <x>0</x> 
    78    <y>0</y> 
    8     <width>489</width> 
    9     <height>392</height> 
     9    <width>535</width> 
     10    <height>493</height> 
    1011   </rect> 
    1112  </property> 
    12   <layout class="QVBoxLayout" > 
    13    <property name="margin" > 
    14     <number>9</number> 
    15    </property> 
    16    <property name="spacing" > 
    17     <number>6</number> 
    18    </property> 
    19    <item> 
    20     <widget class="QListWidget" name="listWidget" /> 
     13  <layout class="QGridLayout" name="gridLayout_2"> 
     14   <item row="0" column="0"> 
     15    <widget class="QListWidget" name="listWidget"/> 
    2116   </item> 
    22    <item> 
    23     <widget class="QLabel" name="label" > 
    24      <property name="text" > 
     17   <item row="1" column="0"> 
     18    <widget class="QGroupBox" name="groupBox"> 
     19     <property name="title"> 
     20      <string>Google Translate</string> 
     21     </property> 
     22     <layout class="QGridLayout" name="gridLayout"> 
     23      <item row="0" column="0"> 
     24       <widget class="QLabel" name="label_2"> 
     25        <property name="text"> 
     26         <string>language to convert the messages received</string> 
     27        </property> 
     28       </widget> 
     29      </item> 
     30      <item row="0" column="1"> 
     31       <widget class="QComboBox" name="comboBox_received"> 
     32        <property name="frame"> 
     33         <bool>true</bool> 
     34        </property> 
     35       </widget> 
     36      </item> 
     37      <item row="1" column="0"> 
     38       <widget class="QLabel" name="label_3"> 
     39        <property name="text"> 
     40         <string>language to convert the messages sent</string> 
     41        </property> 
     42       </widget> 
     43      </item> 
     44      <item row="1" column="1"> 
     45       <widget class="QComboBox" name="comboBox_sent"/> 
     46      </item> 
     47     </layout> 
     48    </widget> 
     49   </item> 
     50   <item row="2" column="0"> 
     51    <widget class="QLabel" name="label"> 
     52     <property name="text"> 
    2553      <string>Your language is not here, do you know that you can do your own translation on Launchpad?</string> 
    2654     </property> 
    27      <property name="wordWrap" > 
     55     <property name="wordWrap"> 
    2856      <bool>true</bool> 
    2957     </property> 
    3058    </widget> 
    3159   </item> 
    32    <item> 
    33     <widget class="LinkLabel" name="launchpadLink" > 
    34      <property name="text" > 
     60   <item row="3" column="0"> 
     61    <widget class="LinkLabel" name="launchpadLink"> 
     62     <property name="text"> 
    3563      <string>More information here.</string> 
    3664     </property> 
  • wengophone/src/presentation/qt/config/QtLanguagesSettings.cpp

    r0 r558  
    4848        _ui->listWidget->addItems(QtLanguage::getAvailableLanguages()); 
    4949 
     50        QStringList list; 
     51                list << "unknow" << "sq"  <<"ar"  <<"bg"  <<"ca" 
     52                <<"zh-CN"   <<"zh-TW"   <<"hr" 
     53                <<"cs"  <<"da"  <<"nl"  <<"en" 
     54                <<"et"  <<"tl"  <<"fi"  <<"fr" 
     55                <<"gl"  <<"de"  <<"el"  <<"iw" 
     56                <<"hi"  <<"hu"  <<"id"  <<"it" 
     57                <<"ja"  <<"ko"  <<"lv"  <<"lt" 
     58                <<"mt"  <<"no"  <<"pl"  <<"pt" 
     59                <<"ro"  <<"ru"  <<"sr"  <<"sk" 
     60                <<"sl"  <<"es"  <<"sv"  <<"th" 
     61                <<"tr"  <<"uk"  <<"vi"; 
     62                 
     63        _ui->comboBox_received->addItems (list); 
     64        _ui->comboBox_sent->addItems (list); 
     65                 
    5066        readConfig(); 
    5167} 
     
    7793        Config & config = ConfigManager::getInstance().getCurrentConfig(); 
    7894        config.set(Config::LANGUAGE_KEY, LanguageList::getISO639Code(language)); 
     95         
     96        config.set(Config::TRANSLATION_SENT_KEY, _ui->comboBox_sent->currentText().toStdString()); 
     97        config.set(Config::TRANSLATION_RECEIVED_KEY, _ui->comboBox_received->currentText().toStdString()); 
    7998} 
    8099 
     
    87106                _ui->listWidget->setCurrentItem(list[0]); 
    88107        } 
     108         
     109        _ui->comboBox_sent->setCurrentIndex(_ui->comboBox_sent->findText(QString::fromStdString(config.getTranslationSent()))); 
     110        _ui->comboBox_received->setCurrentIndex(_ui->comboBox_received->findText(QString::fromStdString(config.getTranslationReceived()))); 
    89111} 
    90112 
Note: See TracChangeset for help on using the changeset viewer.