00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "textedit.h"
00016 #include "filePermissions.h"
00017
00018
00019 #include <opie2/odebug.h>
00020 #include <opie2/ofileselector.h>
00021 #include <opie2/ofiledialog.h>
00022 #include <opie2/ofontselector.h>
00023 #include <opie2/oresource.h>
00024
00025 #include <qpe/config.h>
00026 #include <qpe/qpeapplication.h>
00027
00028
00029
00030 #include <qmenubar.h>
00031 #include <qtoolbar.h>
00032 #include <qtextstream.h>
00033 #include <qclipboard.h>
00034 #include <qaction.h>
00035 #include <qlineedit.h>
00036 #include <qmessagebox.h>
00037 #include <qlayout.h>
00038 #include <qtimer.h>
00039 #include <qdir.h>
00040
00041
00042 #include <unistd.h>
00043 #include <sys/stat.h>
00044 #include <stdlib.h>
00045
00046 using namespace Opie::Core;
00047 using namespace Opie::Ui;
00048
00049 #if QT_VERSION < 0x030000
00050 class QpeEditor : public QMultiLineEdit
00051 {
00052
00053 public:
00054 QpeEditor( QWidget *parent, const char * name = 0 )
00055 : QMultiLineEdit( parent, name ) {
00056 clearTableFlags();
00057 setTableFlags( Tbl_vScrollBar | Tbl_autoHScrollBar );
00058 }
00059
00060 void find( const QString &txt, bool caseSensitive,
00061 bool backwards );
00062 protected:
00063 bool markIt;
00064 int line1, line2, col1, col2;
00065 void mousePressEvent( QMouseEvent * );
00066 void mouseReleaseEvent( QMouseEvent * );
00067
00068
00069
00070
00071
00072
00073
00074
00075 private:
00076
00077 };
00078
00079 void QpeEditor::mousePressEvent( QMouseEvent *e ) {
00080 switch(e->button()) {
00081 case RightButton:
00082 {
00083
00084 this->setSelection( line1, col1, line2, col2);
00085 QMultiLineEdit::mousePressEvent( e );
00086 markIt = false;
00087 }
00088 break;
00089 default:
00090 {
00091 if(!markIt) {
00092 int line, col;
00093 this->getCursorPosition(&line, &col);
00094 line1=line2=line;
00095 col1=col2=col;
00096 }
00097 QMultiLineEdit::mousePressEvent( e );
00098 }
00099 break;
00100 };
00101 }
00102
00103 void QpeEditor::mouseReleaseEvent( QMouseEvent * ) {
00104 if(this->hasMarkedText()) {
00105 markIt = true;
00106 this->getMarkedRegion( &line1, &col1, &line2, & col2 );
00107 } else {
00108 markIt = false;
00109 }
00110 }
00111
00112 void QpeEditor::find ( const QString &txt, bool caseSensitive,
00113 bool backwards )
00114 {
00115 static bool wrap = false;
00116 int line, col;
00117 if ( wrap ) {
00118 if ( !backwards )
00119 line = col = 0;
00120 wrap = false;
00121
00122 } else {
00123 getCursorPosition( &line, &col );
00124 }
00125
00126 if ( !backwards ) {
00127 for ( ; ; ) {
00128 if ( line >= numLines() ) {
00129 wrap = true;
00130
00131 break;
00132 }
00133 int findCol = getString( line )->find( txt, col, caseSensitive );
00134 if ( findCol >= 0 ) {
00135 setCursorPosition( line, findCol, false );
00136 col = findCol + txt.length();
00137 setCursorPosition( line, col, true );
00138
00139
00140 break;
00141 }
00142 line++;
00143 col = 0;
00144 }
00145 }
00146 }
00147
00148
00149 #else
00150
00151 #error "Must make a QpeEditor that inherits QTextEdit"
00152
00153 #endif
00154
00155
00156 static const int nfontsizes = 6;
00157 static const int fontsize[nfontsizes] = {8,10,12,14,18,24};
00158
00159 TextEdit::TextEdit( QWidget *parent, const char *name, WFlags f )
00160 : QMainWindow( parent, name, f ), bFromDocView( false )
00161 {
00162 doc = 0;
00163 edited=false;
00164 fromSetDocument=false;
00165
00166 setToolBarsMovable( false );
00167 connect( qApp,SIGNAL( aboutToQuit()),SLOT( cleanUp()) );
00168
00169 channel = new QCopChannel( "QPE/Application/textedit", this );
00170 connect( channel, SIGNAL(received(const QCString&,const QByteArray&)),
00171 this, SLOT(receive(const QCString&,const QByteArray&)) );
00172
00173 setIcon( Opie::Core::OResource::loadPixmap( "textedit/TextEditor", Opie::Core::OResource::SmallIcon ) );
00174
00175 QToolBar *bar = new QToolBar( this );
00176 bar->setHorizontalStretchable( true );
00177 menu = bar;
00178
00179 QMenuBar *mb = new QMenuBar( bar );
00180 QPopupMenu *file = new QPopupMenu( this );
00181 QPopupMenu *edit = new QPopupMenu( this );
00182 QPopupMenu *advancedMenu = new QPopupMenu(this);
00183
00184 font = new QPopupMenu( this );
00185
00186 bar = new QToolBar( this );
00187 editBar = bar;
00188
00189 QAction *a = new QAction( tr( "New" ), Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
00190 QString::null, 0, this, 0 );
00191 connect( a, SIGNAL( activated() ), this, SLOT( fileNew() ) );
00192
00193 a->addTo( file );
00194
00195 a = new QAction( tr( "Open" ), Opie::Core::OResource::loadPixmap( "fileopen", Opie::Core::OResource::SmallIcon ),
00196 QString::null, 0, this, 0 );
00197 connect( a, SIGNAL( activated() ), this, SLOT( fileOpen() ) );
00198 a->addTo( bar );
00199 a->addTo( file );
00200
00201 a = new QAction( tr( "Save" ), Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon ),
00202 QString::null, 0, this, 0 );
00203 connect( a, SIGNAL( activated() ), this, SLOT( save() ) );
00204 file->insertSeparator();
00205 a->addTo( bar );
00206 a->addTo( file );
00207
00208 a = new QAction( tr( "Save As" ), Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon ),
00209 QString::null, 0, this, 0 );
00210 connect( a, SIGNAL( activated() ), this, SLOT( saveAs() ) );
00211 a->addTo( file );
00212
00213 a = new QAction( tr( "Cut" ), Opie::Core::OResource::loadPixmap( "cut", Opie::Core::OResource::SmallIcon ),
00214 QString::null, 0, this, 0 );
00215 connect( a, SIGNAL( activated() ), this, SLOT( editCut() ) );
00216 a->addTo( editBar );
00217 a->addTo( edit );
00218
00219 a = new QAction( tr( "Copy" ), Opie::Core::OResource::loadPixmap( "copy", Opie::Core::OResource::SmallIcon ),
00220 QString::null, 0, this, 0 );
00221 connect( a, SIGNAL( activated() ), this, SLOT( editCopy() ) );
00222 a->addTo( editBar );
00223 a->addTo( edit );
00224
00225 a = new QAction( tr( "Paste" ), Opie::Core::OResource::loadPixmap( "paste", Opie::Core::OResource::SmallIcon ),
00226 QString::null, 0, this, 0 );
00227 connect( a, SIGNAL( activated() ), this, SLOT( editPaste() ) );
00228 a->addTo( editBar );
00229 a->addTo( edit );
00230
00231
00232 #ifndef QT_NO_CLIPBOARD
00233 a = new QAction( tr( "Insert Time and Date" ), Opie::Core::OResource::loadPixmap( "paste", Opie::Core::OResource::SmallIcon ),
00234 QString::null, 0, this, 0 );
00235 connect( a, SIGNAL( activated() ), this, SLOT( editPasteTimeDate() ) );
00236 a->addTo( edit );
00237 #endif
00238
00239 a = new QAction( tr( "Goto Line..." ), Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ),
00240 QString::null, 0, this, 0 );
00241 connect( a, SIGNAL( activated() ), this, SLOT( gotoLine() ) );
00242 edit->insertSeparator();
00243 a->addTo( edit );
00244
00245 a = new QAction( tr( "Find..." ), Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ),
00246 QString::null, 0, this, 0 );
00247 connect( a, SIGNAL( activated() ), this, SLOT( editFind() ) );
00248 a->addTo( bar );
00249 a->addTo( edit );
00250
00251 zin = new QAction( tr("Zoom in"), QString::null, 0, this, 0 );
00252 connect( zin, SIGNAL( activated() ), this, SLOT( zoomIn() ) );
00253 zin->addTo( font );
00254
00255 zout = new QAction( tr("Zoom out"), QString::null, 0, this, 0 );
00256 connect( zout, SIGNAL( activated() ), this, SLOT( zoomOut() ) );
00257 zout->addTo( font );
00258
00259 font->insertSeparator();
00260
00261 font->insertItem(tr("Font"), this, SLOT(changeFont()) );
00262
00263 font->insertSeparator();
00264 font->insertItem(tr("Advanced Features"), advancedMenu);
00265
00266 QAction *wa = new QAction( tr("Wrap lines"),
00267 QString::null, 0, this, 0 );
00268 connect( wa, SIGNAL( toggled(bool) ),
00269 this, SLOT( setWordWrap(bool) ) );
00270 wa->setToggleAction(true);
00271 wa->addTo( advancedMenu);
00272
00273 nStart = new QAction( tr("Start with new file"),
00274 QString::null, 0, this, 0 );
00275 connect( nStart, SIGNAL( toggled(bool) ),
00276 this, SLOT( changeStartConfig(bool) ) );
00277 nStart->setToggleAction(true);
00278 nStart->addTo( advancedMenu );
00279 nStart->setEnabled(false);
00280
00281 nAdvanced = new QAction( tr("Prompt on Exit"),
00282 QString::null, 0, this, 0 );
00283 connect( nAdvanced, SIGNAL( toggled(bool) ),
00284 this, SLOT( doPrompt(bool) ) );
00285 nAdvanced->setToggleAction(true);
00286 nAdvanced->addTo( advancedMenu );
00287
00288 desktopAction = new QAction( tr("Always open linked file"),
00289 QString::null, 0, this, 0 );
00290 connect( desktopAction, SIGNAL( toggled(bool) ),
00291 this, SLOT( doDesktop(bool) ) );
00292 desktopAction->setToggleAction(true);
00293 desktopAction->addTo( advancedMenu);
00294
00295 filePermAction = new QAction( tr("File Permissions"),
00296 QString::null, 0, this, 0 );
00297 connect( filePermAction, SIGNAL( toggled(bool) ),
00298 this, SLOT( doFilePerms(bool) ) );
00299 filePermAction->setToggleAction(true);
00300 filePermAction->addTo( advancedMenu);
00301
00302 searchBarAction = new QAction( tr("Search Bar Open"),
00303 QString::null, 0, this, 0 );
00304 connect( searchBarAction, SIGNAL( toggled(bool) ),
00305 this, SLOT( setSearchBar(bool) ) );
00306 searchBarAction->setToggleAction(true);
00307 searchBarAction->addTo( advancedMenu);
00308
00309 nAutoSave = new QAction( tr("Auto Save 5 min."),
00310 QString::null, 0, this, 0 );
00311 connect( nAutoSave, SIGNAL( toggled(bool) ),
00312 this, SLOT( doTimer(bool) ) );
00313 nAutoSave->setToggleAction(true);
00314 nAutoSave->addTo( advancedMenu);
00315
00316
00317
00318
00319
00320
00321 mb->insertItem( tr( "File" ), file );
00322 mb->insertItem( tr( "Edit" ), edit );
00323 mb->insertItem( tr( "View" ), font );
00324
00325 searchBar = new QToolBar(this);
00326 addToolBar( searchBar, "Search", QMainWindow::Top, true );
00327
00328 searchBar->setHorizontalStretchable( true );
00329
00330 searchEdit = new QLineEdit( searchBar, "searchEdit" );
00331 searchBar->setStretchableWidget( searchEdit );
00332 connect( searchEdit, SIGNAL( textChanged(const QString&) ),
00333 this, SLOT( search() ) );
00334
00335 a = new QAction( tr( "Find Next" ), Opie::Core::OResource::loadPixmap( "next", Opie::Core::OResource::SmallIcon ),
00336 QString::null, 0, this, 0 );
00337 connect( a, SIGNAL( activated() ), this, SLOT( findNext() ) );
00338 a->addTo( searchBar );
00339 a->addTo( edit );
00340
00341 a = new QAction( tr( "Close Find" ), Opie::Core::OResource::loadPixmap( "close", Opie::Core::OResource::SmallIcon ),
00342 QString::null, 0, this, 0 );
00343 connect( a, SIGNAL( activated() ), this, SLOT( findClose() ) );
00344 a->addTo( searchBar );
00345
00346 edit->insertSeparator();
00347 a = new QAction( tr( "Delete" ), Opie::Core::OResource::loadPixmap( "close", Opie::Core::OResource::SmallIcon ),
00348 QString::null, 0, this, 0 );
00349 connect( a, SIGNAL( activated() ), this, SLOT( editDelete() ) );
00350 a->addTo( edit );
00351
00352 searchBar->hide();
00353
00354 editor = new QpeEditor( this );
00355 setCentralWidget( editor );
00356 editor->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00357 connect( editor, SIGNAL( textChanged() ),
00358 this, SLOT( editorChanged() ) );
00359
00360 QPEApplication::setStylusOperation( editor, QPEApplication::RightOnHold);
00361
00362 Config cfg("TextEdit");
00363 cfg. setGroup ( "Font" );
00364
00365 QFont defaultFont = editor-> font ( );
00366
00367 QString family = cfg. readEntry ( "Family", defaultFont. family ( ));
00368 int size = cfg. readNumEntry ( "Size", defaultFont. pointSize ( ));
00369 int weight = cfg. readNumEntry ( "Weight", defaultFont. weight ( ));
00370 bool italic = cfg. readBoolEntry ( "Italic", defaultFont. italic ( ));
00371
00372 defaultFont = QFont ( family, size, weight, italic );
00373 editor-> setFont ( defaultFont );
00374
00375
00376 cfg.setGroup ( "View" );
00377
00378 promptExit = cfg.readBoolEntry ( "PromptExit", false );
00379 openDesktop = cfg.readBoolEntry ( "OpenDesktop", true );
00380 filePerms = cfg.readBoolEntry ( "FilePermissions", false );
00381 useSearchBar = cfg.readBoolEntry ( "SearchBar", false );
00382 startWithNew = cfg.readBoolEntry ( "startNew", true);
00383 featureAutoSave = cfg.readBoolEntry( "autosave", false);
00384
00385 if(useSearchBar) searchBarAction->setOn(true);
00386 if(promptExit) nAdvanced->setOn( true );
00387 if(openDesktop) desktopAction->setOn( true );
00388 if(filePerms) filePermAction->setOn( true );
00389 if(startWithNew) nStart->setOn( true );
00390 if(featureAutoSave) nAutoSave->setOn(true);
00391
00392
00393
00394
00395
00396 bool wrap = cfg. readBoolEntry ( "Wrap", true );
00397 wa-> setOn ( wrap );
00398 setWordWrap ( wrap );
00399
00401 if( qApp->argc() > 1) {
00402 currentFileName=qApp->argv()[1];
00403
00404 QFileInfo fi(currentFileName);
00405
00406 if(fi.baseName().left(1) == "") {
00407 openDotFile(currentFileName);
00408 } else {
00409 openFile(currentFileName);
00410 }
00411 } else {
00412 edited1=false;
00413 openDotFile("");
00414 }
00415
00416 viewSelection = cfg.readNumEntry( "FileView", 0 );
00417 }
00418
00419 TextEdit::~TextEdit() {
00420 if( edited1 && !promptExit) {
00421 switch( savePrompt() ) {
00422 case 1: {
00423 saveAs();
00424 }
00425 break;
00426 };
00427 }
00428
00429 delete editor;
00430 }
00431
00432 void TextEdit::closeEvent(QCloseEvent *) {
00433 if( promptExit) {
00434 switch( savePrompt() ) {
00435 case 1: {
00436 saveAs();
00437 qApp->quit();
00438 }
00439 break;
00440
00441 case 2: {
00442 qApp->quit();
00443 }
00444 break;
00445
00446 case -1:
00447 break;
00448 };
00449 }
00450 else
00451 qApp->quit();
00452 }
00453
00454 void TextEdit::cleanUp() {
00455
00456 Config cfg ( "TextEdit" );
00457 cfg. setGroup ( "Font" );
00458 QFont f = editor->font();
00459 cfg.writeEntry ( "Family", f. family ( ));
00460 cfg.writeEntry ( "Size", f. pointSize ( ));
00461 cfg.writeEntry ( "Weight", f. weight ( ));
00462 cfg.writeEntry ( "Italic", f. italic ( ));
00463
00464 cfg.setGroup ( "View" );
00465 cfg.writeEntry ( "Wrap", editor->wordWrap() == QMultiLineEdit::WidgetWidth );
00466 cfg.writeEntry ( "FileView", viewSelection );
00467
00468 cfg.writeEntry ( "PromptExit", promptExit );
00469 cfg.writeEntry ( "OpenDesktop", openDesktop );
00470 cfg.writeEntry ( "FilePermissions", filePerms );
00471 cfg.writeEntry ( "SearchBar", useSearchBar );
00472 cfg.writeEntry ( "startNew", startWithNew );
00473
00474 }
00475
00476
00477 void TextEdit::accept() {
00478 if( edited1)
00479 saveAs();
00480 qApp->quit();
00481 }
00482
00483 void TextEdit::zoomIn() {
00484 setFontSize(editor->font().pointSize()+1,false);
00485 }
00486
00487 void TextEdit::zoomOut() {
00488 setFontSize(editor->font().pointSize()-1,true);
00489 }
00490
00491
00492 void TextEdit::setFontSize(int sz, bool round_down_not_up) {
00493 int s=10;
00494 for (int i=0; i<nfontsizes; i++) {
00495 if ( fontsize[i] == sz ) {
00496 s = sz;
00497 break;
00498 } else if ( round_down_not_up ) {
00499 if ( fontsize[i] < sz )
00500 s = fontsize[i];
00501 } else {
00502 if ( fontsize[i] > sz ) {
00503 s = fontsize[i];
00504 break;
00505 }
00506 }
00507 }
00508
00509 QFont f = editor->font();
00510 f.setPointSize(s);
00511 editor->setFont(f);
00512
00513 zin->setEnabled(s != fontsize[nfontsizes-1]);
00514 zout->setEnabled(s != fontsize[0]);
00515 }
00516
00517 void TextEdit::setBold(bool y) {
00518 QFont f = editor->font();
00519 f.setBold(y);
00520 editor->setFont(f);
00521 }
00522
00523 void TextEdit::setItalic(bool y) {
00524 QFont f = editor->font();
00525 f.setItalic(y);
00526 editor->setFont(f);
00527 }
00528
00529 void TextEdit::setWordWrap(bool y) {
00530 bool state = editor->edited();
00531 QString captionStr = caption();
00532 bool b1 = edited1;
00533 bool b2 = edited;
00534
00535 editor->setWordWrap(y ? QMultiLineEdit::WidgetWidth : QMultiLineEdit::NoWrap );
00536 editor->setEdited( state );
00537 edited1=b1;
00538 edited=b2;
00539 setCaption(captionStr);
00540 }
00541
00542 void TextEdit::setSearchBar(bool b) {
00543 useSearchBar=b;
00544 Config cfg("TextEdit");
00545 cfg.setGroup("View");
00546 cfg.writeEntry ( "SearchBar", b );
00547 searchBarAction->setOn(b);
00548 if(b)
00549 searchBar->show();
00550 else
00551 searchBar->hide();
00552 editor->setFocus();
00553 }
00554
00555 void TextEdit::fileNew() {
00556
00557
00558
00559 newFile(DocLnk());
00560 }
00561
00562 void TextEdit::fileOpen() {
00563 Config cfg("TextEdit");
00564 cfg. setGroup ( "View" );
00565 QMap<QString, QStringList> map;
00566 map.insert(tr("All"), QStringList() );
00567 QStringList text;
00568 text << "text/*";
00569 map.insert(tr("Text"), text );
00570 text << "*";
00571 map.insert(tr("All"), text );
00572 QString str = OFileDialog::getOpenFileName( 2,
00573 QString::null ,
00574 QString::null, map);
00575 if( !str.isEmpty() && QFile(str).exists() && !QFileInfo(str).isDir() )
00576 {
00577 openFile( str );
00578 }
00579 else
00580 updateCaption();
00581 }
00582
00583 void TextEdit::doSearchBar() {
00584 if(!useSearchBar)
00585 searchBar->hide();
00586 else
00587 searchBar->show();
00588 }
00589
00590 #if 0
00591 void TextEdit::slotFind() {
00592 FindDialog frmFind( tr("Text Editor"), this );
00593 connect( &frmFind, SIGNAL(signalFindClicked(const QString&,bool,bool,int)),
00594 editor, SLOT(slotDoFind(const QString&,bool,bool)));
00595
00596
00597
00598 connect( editor, SIGNAL(notFound()),
00599 &frmFind, SLOT(slotNotFound()) );
00600 connect( editor, SIGNAL(searchWrapped()),
00601 &frmFind, SLOT(slotWrapAround()) );
00602
00603 frmFind.exec();
00604
00605
00606 }
00607 #endif
00608
00609 void TextEdit::fileRevert() {
00610 clear();
00611 fileOpen();
00612 }
00613
00614 void TextEdit::editCut() {
00615 #ifndef QT_NO_CLIPBOARD
00616 editor->cut();
00617 #endif
00618 }
00619
00620 void TextEdit::editCopy() {
00621 #ifndef QT_NO_CLIPBOARD
00622 editor->copy();
00623 #endif
00624 }
00625
00626 void TextEdit::editPaste() {
00627 #ifndef QT_NO_CLIPBOARD
00628 editor->paste();
00629 #endif
00630 }
00631
00632 void TextEdit::editFind() {
00633 searchBar->show();
00634 searchEdit->setFocus();
00635 }
00636
00637 void TextEdit::findNext() {
00638 editor->find( searchEdit->text(), false, false );
00639
00640 }
00641
00642 void TextEdit::findClose() {
00643 searchBar->hide();
00644 }
00645
00646 void TextEdit::search() {
00647 editor->find( searchEdit->text(), false, false );
00648 }
00649
00650 void TextEdit::newFile( const DocLnk &f ) {
00651 DocLnk nf = f;
00652 nf.setType("text/plain");
00653 clear();
00654 setWState (WState_Reserved1 );
00655 editor->setFocus();
00656 doc = new DocLnk(nf);
00657 currentFileName = "Unnamed";
00658 odebug << "newFile "+currentFileName << oendl;
00659 updateCaption( currentFileName);
00660
00661 }
00662
00663 void TextEdit::openDotFile( const QString &f ) {
00664 if(!currentFileName.isEmpty()) {
00665 currentFileName=f;
00666
00667 odebug << "openFile dotfile " + currentFileName << oendl;
00668 QString txt;
00669 QFile file(f);
00670 file.open(IO_ReadWrite);
00671 QTextStream t(&file);
00672 while ( !t.atEnd()) {
00673 txt+=t.readLine()+"\n";
00674 }
00675 editor->setText(txt);
00676 editor->setEdited( false);
00677 edited1=false;
00678 edited=false;
00679
00680
00681 }
00682 updateCaption( currentFileName);
00683 }
00684
00685 void TextEdit::openFile( const QString &f ) {
00686 odebug << "filename is "+ f << oendl;
00687 QString filer;
00688 QFileInfo fi( f);
00689
00690 if(f.find(".desktop",0,true) != -1 && !openDesktop )
00691 {
00692 switch ( QMessageBox::warning(this,tr("Text Editor"),tr("Text Editor has detected<BR>you selected a <B>.desktop</B>file.<BR>Open<B>.desktop</B> file or <B>linked</B> file?"),tr(".desktop File"),tr("Linked Document"),0,1,1) )
00693 {
00694 case 0:
00695 filer = f;
00696 break;
00697 case 1:
00698 DocLnk sf(f);
00699 filer = sf.file();
00700 break;
00701 };
00702 }
00703 else if(fi.baseName().left(1) == "")
00704 {
00705 odebug << "opening dotfile" << oendl;
00706 currentFileName=f;
00707 openDotFile(currentFileName);
00708 return;
00709 }
00710
00711
00712
00713
00714 else if (f.find(".desktop",0,true) != -1 )
00715 {
00716 DocLnk sf(f);
00717 filer = sf.file();
00718 if(filer.right(1) == "/")
00719 filer = f;
00720
00721 }
00722 else
00723 filer = f;
00724
00725 DocLnk nf;
00726 nf.setType("text/plain");
00727 nf.setFile(filer);
00728 currentFileName=filer;
00729
00730 nf.setName(fi.baseName());
00731 openFile(nf);
00732
00733 odebug << "openFile string "+currentFileName << oendl;
00734
00735 showEditTools();
00736
00737 QString name = filer;
00738 int sep = name.findRev( '/' );
00739 if ( sep > 0 )
00740 name = name.mid( sep+1 );
00741 updateCaption( name );
00742 }
00743
00744 void TextEdit::openFile( const DocLnk &f ) {
00745
00746
00747 FileManager fm;
00748 QString txt;
00749 currentFileName=f.file();
00750 odebug << "openFile doclnk " + currentFileName << oendl;
00751 if ( !fm.loadFile( f, txt ) ) {
00752
00753 odebug << "Cannot open file" << oendl;
00754 }
00755
00756 if ( doc )
00757 delete doc;
00758 doc = new DocLnk(f);
00759 editor->setText(txt);
00760 editor->setEdited( false);
00761 edited1=false;
00762 edited=false;
00763
00764 doc->setName(currentFileName);
00765 updateCaption();
00766 setTimer();
00767 }
00768
00769 void TextEdit::showEditTools() {
00770 menu->show();
00771 editBar->show();
00772 if(!useSearchBar)
00773 searchBar->hide();
00774 else
00775 searchBar->show();
00776 setWState (WState_Reserved1 );
00777 }
00778
00781 bool TextEdit::save() {
00782 QString name, file;
00783 odebug << "saveAsFile " + currentFileName << oendl;
00784 if(currentFileName.isEmpty()) {
00785 saveAs();
00786 return false;
00787 }
00788 name = currentFileName;
00789 if(doc) {
00790 file = doc->file();
00791 odebug << "saver file "+file << oendl;
00792 name = doc->name();
00793 odebug << "File named "+name << oendl;
00794 } else {
00795 file = currentFileName;
00796 name = QFileInfo(currentFileName).baseName();
00797 }
00798
00799 QString rt = editor->text();
00800 if( !rt.isEmpty() ) {
00801 if(name.isEmpty()) {
00802 saveAs();
00803 } else {
00804 currentFileName = name;
00805 odebug << "saveFile "+currentFileName << oendl;
00806
00807 struct stat buf;
00808 mode_t mode;
00809 stat(file.latin1(), &buf);
00810 mode = buf.st_mode;
00811
00812 if(!fileIs) {
00813 doc->setName( name);
00814 FileManager fm;
00815 if ( !fm.saveFile( *doc, rt ) ) {
00816 QMessageBox::message(tr("Text Edit"),tr("Save Failed"));
00817 return false;
00818 }
00819 } else {
00820 odebug << "regular save file" << oendl;
00821 QFile f(file);
00822 if( f.open(IO_WriteOnly)) {
00823 QCString crt = rt.utf8();
00824 f.writeBlock(crt,crt.length());
00825 } else {
00826 QMessageBox::message(tr("Text Edit"),tr("Write Failed"));
00827 return false;
00828 }
00829
00830 }
00831 editor->setEdited( false);
00832 edited1=false;
00833 edited=false;
00834 if(caption().left(1)=="*")
00835 setCaption(caption().right(caption().length()-1));
00836
00837
00838 chmod( file.latin1(), mode);
00839 }
00840 return true;
00841 }
00842 return false;
00843 }
00844
00847 bool TextEdit::saveAs() {
00848
00849 if(caption() == tr("Text Editor"))
00850 return false;
00851 odebug << "saveAsFile " + currentFileName << oendl;
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864 QString rt = editor->text();
00865 odebug << currentFileName << oendl;
00866
00867 if( currentFileName.isEmpty()
00868 || currentFileName == tr("Unnamed")
00869 || currentFileName == tr("Text Editor")) {
00870 odebug << "do silly TT filename thing" << oendl;
00871
00872 QString pt = rt.simplifyWhiteSpace();
00873 int i = pt.find( ' ' );
00874 QString docname = pt;
00875 if ( i > 0 )
00876 docname = pt.left( i );
00877
00878 while( docname.startsWith( "." ) )
00879 docname = docname.mid( 1 );
00880 docname.replace( QRegExp("/"), "_" );
00881
00882
00883 if ( docname.length() > 40 )
00884 docname = docname.left(40);
00885 if ( docname.isEmpty() )
00886 docname = tr("Unnamed");
00887 if(doc) doc->setName(docname);
00888 currentFileName=docname;
00889
00890
00891
00892 }
00893
00894
00895 QMap<QString, QStringList> map;
00896 map.insert(tr("All"), QStringList() );
00897 QStringList text;
00898 text << "text/*";
00899 map.insert(tr("Text"), text );
00900 text << "*";
00901 map.insert(tr("All"), text );
00902
00903 QFileInfo cuFi( currentFileName);
00904 QString filee = cuFi.fileName();
00905 QString dire = cuFi.dirPath();
00906 if(dire==".")
00907 dire = QPEApplication::documentDir();
00908 QString str;
00909 if( !featureAutoSave) {
00910 str = OFileDialog::getSaveFileName( 2,
00911 dire,
00912 filee, map);
00913 } else
00914 str = currentFileName;
00915
00916 if(!str.isEmpty()) {
00917 QString fileNm=str;
00918
00919 odebug << "saving filename "+fileNm << oendl;
00920 QFileInfo fi(fileNm);
00921 currentFileName=fi.fileName();
00922 if(doc)
00923
00924
00925 delete doc;
00926 DocLnk nf;
00927 nf.setType("text/plain");
00928 nf.setFile( fileNm);
00929 doc = new DocLnk(nf);
00930
00931 odebug << "Saving file as "+currentFileName << oendl;
00932 doc->setName( fi.baseName() );
00933 updateCaption( currentFileName);
00934
00935 FileManager fm;
00936 if ( !fm.saveFile( *doc, rt ) ) {
00937 QMessageBox::message(tr("Text Edit"),tr("Save Failed"));
00938 return false;
00939 }
00940
00941 if( filePerms ) {
00942 filePermissions *filePerm;
00943 filePerm = new filePermissions(this,
00944 tr("Permissions"),true,
00945 0,(const QString &)fileNm);
00946 QPEApplication::execDialog( filePerm );
00947
00948 if( filePerm)
00949 delete filePerm;
00950 }
00951
00952 editor->setEdited( false);
00953 edited1 = false;
00954 edited = false;
00955 if(caption().left(1)=="*")
00956 setCaption(caption().right(caption().length()-1));
00957
00958 return true;
00959 }
00960 odebug << "returning false" << oendl;
00961 currentFileName = "";
00962 return false;
00963 }
00964
00965 void TextEdit::clear() {
00966 delete doc;
00967 doc = 0;
00968 editor->clear();
00969 }
00970
00971 void TextEdit::updateCaption( const QString &name ) {
00972
00973 if ( name.isEmpty() )
00974 setCaption( tr("Text Editor") );
00975 else {
00976 QString s = name;
00977 if ( s.isNull() )
00978 s = doc->name();
00979 if ( s.isEmpty() ) {
00980 s = tr( "Unnamed" );
00981 currentFileName=s;
00982 }
00983
00984
00985 setCaption( tr("%1 - Text Editor").arg( s ) );
00986 }
00987 }
00988
00989 void TextEdit::setDocument(const QString& fileref) {
00990 if(fileref != "Unnamed") {
00991 currentFileName=fileref;
00992 odebug << "setDocument" << oendl;
00993 QFileInfo fi(currentFileName);
00994 odebug << "basename:"+fi.baseName()+": current filenmame "+currentFileName << oendl;
00995 if( (fi.baseName().left(1)).isEmpty() ) {
00996 openDotFile(currentFileName);
00997
00998 } else {
00999 odebug << "setDoc open" << oendl;
01000 bFromDocView = true;
01001 openFile(fileref);
01002 editor->setEdited(true);
01003 edited1=false;
01004 edited=true;
01005
01006
01007 }
01008 }
01009 updateCaption( currentFileName);
01010 }
01011
01012 void TextEdit::changeFont() {
01013 QDialog *d = new QDialog ( this, "FontDialog", true );
01014 d-> setCaption ( tr( "Choose font" ));
01015 QBoxLayout *lay = new QVBoxLayout ( d );
01016 OFontSelector *ofs = new OFontSelector ( true, d );
01017 lay-> addWidget ( ofs );
01018 ofs-> setSelectedFont ( editor-> font ( ));
01019
01020 if ( QPEApplication::execDialog( d ) == QDialog::Accepted )
01021 editor-> setFont ( ofs-> selectedFont ( ));
01022 delete d;
01023
01024 }
01025
01026 void TextEdit::editDelete() {
01027 switch ( QMessageBox::warning(this,tr("Text Editor"),
01028 tr("Do you really want<BR>to <B>delete</B> "
01029 "the current file\nfrom the disk?<BR>This is "
01030 "<B>irreversable!</B>"),
01031 tr("Yes"),tr("No"),0,0,1) ) {
01032 case 0:
01033 if(doc) {
01034 doc->removeFiles();
01035 clear();
01036 setCaption( tr("Text Editor") );
01037 }
01038 break;
01039 case 1:
01040
01041 break;
01042 };
01043 }
01044
01045 void TextEdit::changeStartConfig( bool b ) {
01046 startWithNew=b;
01047 Config cfg("TextEdit");
01048 cfg.setGroup("View");
01049 cfg.writeEntry("startNew",b);
01050 update();
01051 }
01052
01053 void TextEdit::editorChanged() {
01054
01055 if( !edited1) {
01056 setCaption( "*"+caption());
01057 edited1=true;
01058 }
01059 edited=true;
01060 }
01061
01062 void TextEdit::receive(const QCString&msg, const QByteArray &) {
01063 odebug << "QCop "+msg << oendl;
01064 if ( msg == "setDocument(QString)" ) {
01065 odebug << "bugger all" << oendl;
01066
01067 }
01068
01069 }
01070
01071 void TextEdit::doAbout() {
01072 QMessageBox::about(0,tr("Text Edit"),tr("Text Edit is copyright<BR>"
01073 "2000 Trolltech AS, and<BR>"
01074 "2002 by <B>L. J. Potter <BR>llornkcor@handhelds.org</B><BR>"
01075 "and is licensed under the GPL"));
01076 }
01077
01078 void TextEdit::doPrompt(bool b) {
01079 promptExit=b;
01080 Config cfg("TextEdit");
01081 cfg.setGroup ( "View" );
01082 cfg.writeEntry ( "PromptExit", b);
01083 }
01084
01085 void TextEdit::doDesktop(bool b) {
01086 openDesktop=b;
01087 Config cfg("TextEdit");
01088 cfg.setGroup ( "View" );
01089 cfg.writeEntry ( "OpenDesktop", b);
01090 }
01091
01092 void TextEdit::doFilePerms(bool b) {
01093 filePerms=b;
01094 Config cfg("TextEdit");
01095 cfg.setGroup ( "View" );
01096 cfg.writeEntry ( "FilePermissions", b);
01097 }
01098
01099 void TextEdit::editPasteTimeDate() {
01100 #ifndef QT_NO_CLIPBOARD
01101 QClipboard *cb = QApplication::clipboard();
01102 QDateTime dt = QDateTime::currentDateTime();
01103 cb->setText( dt.toString());
01104 editor->paste();
01105 #endif
01106 }
01107
01108 int TextEdit::savePrompt()
01109 {
01110 switch( QMessageBox::information( 0, (tr("Textedit")),
01111 (tr("Textedit detected\n"
01112 "you have unsaved changes\n"
01113 "Go ahead and save?\n")),
01114 (tr("Save")), (tr("Don't Save")), (tr("&Cancel")), 2, 2 ) )
01115 {
01116 case 0:
01117 {
01118 return 1;
01119 }
01120 break;
01121
01122 case 1:
01123 {
01124 return 2;
01125 }
01126 break;
01127
01128 case 2:
01129 {
01130 return -1;
01131 }
01132 break;
01133 };
01134
01135 return 0;
01136 }
01137
01138 void TextEdit::timerCrank()
01139 {
01140 if(featureAutoSave && edited1)
01141 {
01142 if(currentFileName.isEmpty())
01143 {
01144 currentFileName = QDir::homeDirPath()+"/textedit.tmp";
01145 saveAs();
01146 }
01147 else
01148 {
01149
01150 save();
01151 }
01152 setTimer();
01153 }
01154 }
01155
01156 void TextEdit::doTimer(bool b)
01157 {
01158 Config cfg("TextEdit");
01159 cfg.setGroup ( "View" );
01160 cfg.writeEntry ( "autosave", b);
01161 featureAutoSave = b;
01162 nAutoSave->setOn(b);
01163 if(b)
01164 {
01165
01166 setTimer();
01167 }
01168
01169
01170 }
01171
01172 void TextEdit::setTimer()
01173 {
01174 if(featureAutoSave)
01175 {
01176
01177 QTimer *timer = new QTimer(this );
01178 connect( timer, SIGNAL(timeout()), this, SLOT(timerCrank()) );
01179 timer->start( 300000, true);
01180 }
01181 }
01182
01183 void TextEdit::gotoLine() {
01184 if( editor->length() < 1)
01185 return;
01186 QWidget *d = QApplication::desktop();
01187 gotoEdit = new QLineEdit( 0, "Goto line");
01188
01189 gotoEdit->move( (d->width()/2) - ( gotoEdit->width()/2) , (d->height()/2) - (gotoEdit->height()/2));
01190 gotoEdit->setFrame(true);
01191 gotoEdit->show();
01192 connect (gotoEdit,SIGNAL(returnPressed()), this, SLOT(doGoto()));
01193 }
01194
01195 void TextEdit::doGoto() {
01196 QString number = gotoEdit->text();
01197 gotoEdit->hide();
01198
01199 if(gotoEdit) {
01200 delete gotoEdit;
01201 gotoEdit = 0;
01202 }
01203
01204 bool ok;
01205 int lineNumber = number.toInt(&ok, 10);
01206 if( editor->numLines() < lineNumber)
01207 QMessageBox::message(tr("Text Edit"),tr("Not enough lines"));
01208 else
01209 {
01210 editor->setCursorPosition(lineNumber, 0, false);
01211 }
01212 }