~ubuntu-branches/ubuntu/lucid/ksocrat/lucid

« back to all changes in this revision

Viewing changes to ksocrat/ksocrat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-12-17 16:24:49 UTC
  • mfrom: (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20051217162449-aqynhynf129nqgmi
Tags: 3.2.1-1build1
Rebuild KDE universe for libstdc++ transition

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    KSocrat
3
 
    Version 3.1.2, 28 April 2003    
4
 
    (C) 2000-2003 Zavolzhsky Alexandr <zavolzhsky@mail.ru>
 
3
    Version 3.2.1, 17 July 2004
 
4
    (C) 2000-2004 Zavolzhsky Alexandr <zavolzhsky@mail.ru>
5
5
 
6
6
    This program is free software; you can redistribute it and/or modify
7
7
    it under the terms of the GNU General Public License as published by
21
21
#include <qaccel.h>
22
22
#include <qfile.h>
23
23
#include <qtextcodec.h>
 
24
#include <qclipboard.h>
24
25
#include <kapp.h>
25
 
#include <kconfig.h>
26
26
#include <kstddirs.h>
27
27
#include <kfiledialog.h>
28
28
#include <klocale.h>
29
29
#include <kmessagebox.h>
30
30
#include <kaboutapplication.h>
31
31
#include <kglobalaccel.h>
32
 
#include <ctype.h>
 
32
#include <kwin.h>
33
33
#include "dockwidget.h"
34
34
#include "ksocrat.h"
35
35
 
38
38
KSocrat::KSocrat(const char *name) : KMainWindow(0L,name){
39
39
    initDialog();
40
40
    readConfig(true);
41
 
    sconfigure = new SConfigure();
 
41
    sconfigure = new SConfigure(this);
42
42
 
43
43
    dw = new DockWidget(this);
44
44
    
45
 
    bGrepFree = true;
46
 
 
47
45
    connect(edWord,SIGNAL(returnPressed()),SLOT(translate()));
48
46
    connect(btClear,SIGNAL(clicked()),this,SLOT(clearall()));
 
47
    connect(btTrClip,SIGNAL(clicked()),this,SLOT(translateClipboard()));
49
48
    connect(btTranslate,SIGNAL(clicked()),this,SLOT(translate()));
 
49
    connect(btTranslate,SIGNAL(clicked()),edWord,SLOT(returnPressed()));
50
50
    connect(btSave,SIGNAL(clicked()),this,SLOT(save()));        
51
51
    connect(btAbout,SIGNAL(clicked()),this,SLOT(about()));              
52
52
    connect(btConfigure,SIGNAL(clicked()),this,SLOT(configure()));
53
 
    connect(btExit,SIGNAL(clicked()),this,SLOT(quit()));
54
 
    connect(cbSentTrans,SIGNAL(clicked()),this,SLOT(setKeepTransState()));
 
53
    connect(btExit,SIGNAL(clicked()),kapp,SLOT(quit()));
55
54
    connect(kapp,SIGNAL(shutDown()),this,SLOT(appClosing()));
56
55
    connect(btHide,SIGNAL(clicked()),this,SLOT(hide()));
57
56
 
64
63
    skey->connectItem(skey->insertItem(ALT+Key_S),this,SLOT(save()));   
65
64
    skey->connectItem(skey->insertItem(ALT+Key_A),this,SLOT(about()));  
66
65
    skey->connectItem(skey->insertItem(ALT+Key_P),this,SLOT(configure()));      
67
 
    skey->connectItem(skey->insertItem(ALT+Key_X),this,SLOT(quit()));
 
66
    skey->connectItem(skey->insertItem(ALT+Key_X),kapp,SLOT(quit()));
68
67
    skey->connectItem(skey->insertItem(ALT+Key_C),this,SLOT(clearall()));
69
68
    skey->connectItem(skey->insertItem(ALT+Key_H),this,SLOT(hide()));
70
69
 
85
84
    if (!sWord.isEmpty() && checkDicPath()) {
86
85
        
87
86
        if (cbAutoLanguage->isChecked()) {
88
 
          const char *c = sWord.data();
89
 
          if(isalpha(c[0])) {
 
87
          if(sWord.at(0).latin1()) {
90
88
            rbEnglish->setChecked(true);
91
89
            }
92
90
          else {
102
100
            sDic=sRuEnDic;
103
101
        }
104
102
 
 
103
        if (!cbKeepTrans->isChecked()) {
 
104
          meResult->clear();
 
105
        }
 
106
 
105
107
        if (cbSentTrans->isChecked()) {
106
 
          meResult->insertLine(i18n("TRANSLATING: ") + sWord);
107
 
          meResult->insertLine("====================");
108
 
          QStringList sl;
109
 
          sl = QStringList::split(" ",sWord,false);
110
 
 
111
 
          for ( QStringList::Iterator it = sl.begin(); it != sl.end(); ++it ) {
112
 
            QString sWord(*it);
113
 
            QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
114
 
            QCString csWord = codec->fromUnicode(sWord);
115
 
 
116
 
            prGrep.clearArguments();
117
 
            prGrep << "grep" << "\"^"+ csWord + "\\ --" + "\"" << sDic;
118
 
 
119
 
            bGrepFree = false;
120
 
            if (!prGrep.start(KProcess::NotifyOnExit,KProcess::Stdout)) {
121
 
              KMessageBox::error(this,i18n("Cannot execute grep utility!"),i18n("Error"));
122
 
              break;
123
 
            }
124
 
 
125
 
            while (!bGrepFree) {
126
 
 
127
 
              if (!prGrep.isRunning()) {
128
 
                if (prGrep.exitStatus() != 0) {
129
 
                  break;
130
 
                }
131
 
              }
132
 
 
133
 
              kapp->processEvents();
134
 
              sleep(1);
135
 
            }
136
 
 
137
 
          }
 
108
          meResult->append(i18n("TRANSLATING: ") + sWord);
 
109
          meResult->append("====================");
 
110
          if(!grepSentence(true))
 
111
            btTranslate->setEnabled(true);
138
112
        }
139
 
        
140
113
        else {
141
 
          QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
142
 
          QCString csWord = codec->fromUnicode(sWord);
143
 
          
144
 
          prGrep.clearArguments();
145
 
          prGrep << "grep" << "\"^"+ csWord +"\"" << sDic;
146
 
 
147
 
          if (!cbKeepTrans->isChecked()) {
148
 
            meResult->clear();
149
 
          }
150
 
 
151
 
          bGrepFree = false;
152
 
          if (!prGrep.start(KProcess::NotifyOnExit,KProcess::Stdout)) {
153
 
              KMessageBox::error(this,i18n("Cannot execute grep utility!"),i18n("Error"));
154
 
          }
 
114
          if(!grep())
 
115
            btTranslate->setEnabled(true);
155
116
        }
156
117
        
157
118
    }
158
119
}
159
120
 
160
121
void KSocrat::save(){
161
 
    QString sName = KFileDialog::getSaveFileName("/");
 
122
    QString sName = KFileDialog::getSaveFileName(QDir::homeDirPath());
162
123
    if (!sName.isEmpty()) {
163
124
        if (sName.right(1)=="/") {
164
125
            KMessageBox::error(this,i18n("You need to specify name of the file."),i18n("Warning"));
168
129
            int iSize = (meResult->text()).length();
169
130
            if (fData.open(IO_WriteOnly)) {
170
131
                QString sText = meResult->text();
171
 
                QTextCodec* codec = QTextCodec::codecForName("KOI8-R");
 
132
                QTextCodec* codec = QTextCodec::codecForLocale();
172
133
                fData.writeBlock(codec->fromUnicode(sText),iSize);
173
134
                fData.close();
174
135
            }
180
141
}
181
142
 
182
143
void KSocrat::done(){
183
 
    btTranslate->setEnabled(true);
 
144
    if(!sMsgs.isEmpty()) {
 
145
      meResult->append(sMsgs);
 
146
      meResult->append("--------------------------------");
 
147
      sMsgs = "";
 
148
    }
 
149
    if(iMultiPass) {
 
150
      grepSentence(false);
 
151
    }
 
152
    else {
 
153
      btTranslate->setEnabled(true);
 
154
    }
184
155
}
185
156
 
186
157
void KSocrat::gotResult(KProcess *,char *msg, int len){
187
 
    QString sMsg;
188
158
    QTextCodec* codec = QTextCodec::codecForName("KOI8-R");
189
 
    sMsg = codec->toUnicode(msg,len);
190
 
    
191
 
    meResult->insertLine(sMsg);
192
 
    meResult->insertLine("--------------------------------");
193
 
    bGrepFree = true;
 
159
    sMsgs += codec->toUnicode(msg,len);
 
160
    edWord->setFocus();
 
161
    edWord->lineEdit()->home(false);
 
162
    edWord->lineEdit()->end(true);
194
163
}
195
164
 
196
165
void KSocrat::gotError(KProcess *,char *msg,int len){
197
166
    QString sMsg;
198
 
    QTextCodec* codec = QTextCodec::codecForName("KOI8-R");
 
167
    QTextCodec* codec = QTextCodec::codecForLocale();
199
168
    sMsg = codec->toUnicode(msg,len);
200
169
    KMessageBox::information(this,sMsg,i18n("Warning"));
201
 
    bGrepFree = true;
202
170
}
203
171
 
204
172
void KSocrat::about(){
212
180
    readConfig(false);
213
181
}
214
182
 
215
 
void KSocrat::quit(){
216
 
    saveHistory();
217
 
    kapp->quit();
218
 
}
219
 
 
220
183
void KSocrat::readConfig(bool bSetFont){
221
184
    config = kapp->config();
222
185
    
263
226
    edWord->setFocus();
264
227
}
265
228
 
266
 
void KSocrat::setKeepTransState() {
267
 
  if (cbSentTrans->isChecked()) {
268
 
    cbKeepTrans->setChecked(true);
269
 
    cbKeepTrans->setEnabled(false);
270
 
  }
271
 
  else {
272
 
    cbKeepTrans->setChecked(false);
273
 
    cbKeepTrans->setEnabled(true);    
274
 
  }
275
 
}
276
 
 
277
229
void KSocrat::appClosing() {
278
230
  saveHistory();
 
231
  saveMainWindowSettings(kapp->config(), "MainWindow");
279
232
}
280
233
 
281
234
void KSocrat::saveHistory() {
284
237
 
285
238
    QStringList slHistory;
286
239
    for(int i = 0; i<= (edWord->count()) - 1; i++) {
287
 
       slHistory.append(edWord->text(i));
 
240
       QString curText = edWord->text(i);
 
241
       if (slHistory.findIndex(curText) == -1) {
 
242
          slHistory.append(curText);
 
243
       }
288
244
    }
289
245
    config->writeEntry("History",slHistory);
290
246
    config->sync();
291
247
}
292
248
 
293
 
void KSocrat::resizeEvent (QResizeEvent * ) {
294
 
  meResult->setGeometry(5,115,this->width() - 10,this->height() - 160);
295
 
  btSave->setGeometry(5,this->height() - 32,60,24);
296
 
  btConfigure->setGeometry(70,this->height() - 32,60,24);
297
 
  btAbout->setGeometry(135,this->height() - 32,60,24);
298
 
  btHide->setGeometry(200,this->height() - 32,50,24);
299
 
  btExit->setGeometry(255,this->height() - 32,50,24);
300
 
}
301
 
 
302
249
void KSocrat::show_hide(){
303
250
  if (this->isVisible()) {
304
251
    this->hide();
305
252
  }
306
 
  else {
 
253
  else {    
307
254
    this->show();
308
 
  }
 
255
    KWin::setActiveWindow(winId());
 
256
  }
 
257
}
 
258
 
 
259
bool KSocrat::grep() {
 
260
  bool bResult;
 
261
 
 
262
  QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
 
263
  QCString csWord = codec->fromUnicode(sWord);
 
264
          
 
265
  prGrep.clearArguments();
 
266
  prGrep << "grep" << "^" + csWord << sDic;
 
267
 
 
268
  iMultiPass = 0;
 
269
  bResult = prGrep.start(KProcess::NotifyOnExit,KProcess::AllOutput);
 
270
  if (!bResult) {
 
271
    KMessageBox::error(this,i18n("Cannot execute grep utility!"),i18n("Error"));
 
272
  }
 
273
 
 
274
  return bResult;
 
275
}
 
276
 
 
277
bool KSocrat::grepSentence(bool bFirstTime) {
 
278
  static QStringList sl;
 
279
  static QStringList::Iterator it;
 
280
  bool bResult;
 
281
  
 
282
  if(bFirstTime) {
 
283
    sl = QStringList::split(" ", sWord, false);
 
284
    it = sl.begin();
 
285
    iMultiPass = sl.count();
 
286
  }
 
287
 
 
288
  if(it == sl.end()) {
 
289
    return false;
 
290
  }
 
291
 
 
292
  QString sWord(*it++);
 
293
  QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
 
294
  QCString csWord = codec->fromUnicode(sWord);
 
295
 
 
296
  prGrep.clearArguments();
 
297
  prGrep << "grep" << "^" + csWord + " --" << sDic;
 
298
 
 
299
  iMultiPass--;
 
300
  bResult = prGrep.start(KProcess::NotifyOnExit,KProcess::AllOutput);
 
301
  if (!bResult) {
 
302
    KMessageBox::error(this,i18n("Cannot execute grep utility!"),i18n("Error"));
 
303
  }
 
304
 
 
305
  return bResult;
 
306
}
 
307
 
 
308
void KSocrat::polish() {
 
309
  QWidget::polish();
 
310
  kapp->sendPostedEvents();
 
311
 
 
312
  //  Set default window size to fit 12 lines of text
 
313
  resize(minimumSizeHint().width(), height() +
 
314
        (meResult->fontMetrics().lineSpacing() * 12 + 6 - meResult->height()));
 
315
 
 
316
  applyMainWindowSettings(kapp->config(), "MainWindow");
 
317
 
 
318
  QRect scr = QApplication::desktop()->screenGeometry();
 
319
  move(scr.center() - rect().center());
 
320
}
 
321
 
 
322
void KSocrat::translateClipboard() {
 
323
    edWord->clearEdit();
 
324
    meResult->clear();
 
325
    QClipboard *cb = kapp->clipboard();
 
326
    edWord->insertItem(cb->text(QClipboard::Clipboard),0);
 
327
    translate();
309
328
}