summaryrefslogtreecommitdiff
authorllornkcor <llornkcor>2003-02-18 19:09:14 (UTC)
committer llornkcor <llornkcor>2003-02-18 19:09:14 (UTC)
commit4dd3080c36c96c58564a29f1b3d2cf21bfb1eae2 (patch) (side-by-side diff)
treefbc89396011e0e9a0dc04fcd1454ee670d6fcae3
parent50f0a50a14f87960cf9492d01aeed5a56a6dca06 (diff)
downloadopie-4dd3080c36c96c58564a29f1b3d2cf21bfb1eae2.zip
opie-4dd3080c36c96c58564a29f1b3d2cf21bfb1eae2.tar.gz
opie-4dd3080c36c96c58564a29f1b3d2cf21bfb1eae2.tar.bz2
autosave feature. will save to ~/textedit.tmp is no filename has been set. moved stuff from public methods
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--core/apps/textedit/textedit.cpp127
-rw-r--r--core/apps/textedit/textedit.h16
2 files changed, 136 insertions, 7 deletions
diff --git a/core/apps/textedit/textedit.cpp b/core/apps/textedit/textedit.cpp
index e931f01..1e8ce7f 100644
--- a/core/apps/textedit/textedit.cpp
+++ b/core/apps/textedit/textedit.cpp
@@ -28,48 +28,50 @@
#include <qpe/qpeapplication.h>
#include <qpe/qpemenubar.h>
#include <qpe/qpetoolbar.h>
#include <qpe/qcopenvelope_qws.h>
#include <qpoint.h>
#include <qtextstream.h>
#include <qdatetime.h>
#include <qclipboard.h>
#include <qstringlist.h>
#include <qaction.h>
#include <qcolordialog.h>
#include <qfileinfo.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qobjectlist.h>
#include <qpopupmenu.h>
#include <qspinbox.h>
#include <qtoolbutton.h>
#include <qwidgetstack.h>
#include <qcheckbox.h>
#include <qcombo.h>
#include <qlayout.h>
#include <qapplication.h>
+#include <qtimer.h>
+#include <qdir.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h> //getenv
/* XPM */
static char * filesave_xpm[] = {
"16 16 78 1",
" c None",
". c #343434",
"+ c #A0A0A0",
"@ c #565656",
"# c #9E9E9E",
"$ c #525252",
"% c #929292",
"& c #676767",
"* c #848484",
"= c #666666",
"- c #D8D8D8",
"; c #FFFFFF",
"> c #DBDBDB",
", c #636363",
"' c #989898",
") c #2D2D2D",
@@ -389,48 +391,55 @@ TextEdit::TextEdit( QWidget *parent, const char *name, WFlags f )
nAdvanced->setToggleAction(true);
nAdvanced->addTo( advancedMenu );
desktopAction = new QAction( tr("Always open linked file"),
QString::null, 0, this, 0 );
connect( desktopAction, SIGNAL( toggled(bool) ),
this, SLOT( doDesktop(bool) ) );
desktopAction->setToggleAction(true);
desktopAction->addTo( advancedMenu);
filePermAction = new QAction( tr("File Permissions"),
QString::null, 0, this, 0 );
connect( filePermAction, SIGNAL( toggled(bool) ),
this, SLOT( doFilePerms(bool) ) );
filePermAction->setToggleAction(true);
filePermAction->addTo( advancedMenu);
searchBarAction = new QAction( tr("Search Bar Open"),
QString::null, 0, this, 0 );
connect( searchBarAction, SIGNAL( toggled(bool) ),
this, SLOT( setSearchBar(bool) ) );
searchBarAction->setToggleAction(true);
searchBarAction->addTo( advancedMenu);
+ nAutoSave = new QAction( tr("Auto Save 5 min."),
+ QString::null, 0, this, 0 );
+ connect( nAutoSave, SIGNAL( toggled(bool) ),
+ this, SLOT( doTimer(bool) ) );
+ nAutoSave->setToggleAction(true);
+ nAutoSave->addTo( advancedMenu);
+
font->insertSeparator();
font->insertItem(tr("About"), this, SLOT( doAbout()) );
mb->insertItem( tr( "File" ), file );
mb->insertItem( tr( "Edit" ), edit );
mb->insertItem( tr( "View" ), font );
searchBar = new QPEToolBar(this);
addToolBar( searchBar, "Search", QMainWindow::Top, true );
searchBar->setHorizontalStretchable( true );
searchEdit = new QLineEdit( searchBar, "searchEdit" );
searchBar->setStretchableWidget( searchEdit );
connect( searchEdit, SIGNAL( textChanged( const QString & ) ),
this, SLOT( search() ) );
a = new QAction( tr( "Find Next" ), Resource::loadPixmap( "next" ),
QString::null, 0, this, 0 );
connect( a, SIGNAL( activated() ), this, SLOT( findNext() ) );
a->addTo( searchBar );
a->addTo( edit );
@@ -457,87 +466,115 @@ TextEdit::TextEdit( QWidget *parent, const char *name, WFlags f )
QPEApplication::setStylusOperation( editor, QPEApplication::RightOnHold);
Config cfg("TextEdit");
cfg. setGroup ( "Font" );
QFont defaultFont = editor-> font ( );
QString family = cfg. readEntry ( "Family", defaultFont. family ( ));
int size = cfg. readNumEntry ( "Size", defaultFont. pointSize ( ));
int weight = cfg. readNumEntry ( "Weight", defaultFont. weight ( ));
bool italic = cfg. readBoolEntry ( "Italic", defaultFont. italic ( ));
defaultFont = QFont ( family, size, weight, italic );
editor-> setFont ( defaultFont );
// updateCaption();
cfg.setGroup ( "View" );
promptExit = cfg.readBoolEntry ( "PromptExit", false );
openDesktop = cfg.readBoolEntry ( "OpenDesktop", true );
filePerms = cfg.readBoolEntry ( "FilePermissions", false );
useSearchBar = cfg.readBoolEntry ( "SearchBar", false );
startWithNew = cfg.readBoolEntry ( "startNew", true);
+ featureAutoSave = cfg.readBoolEntry( "autosave", false);
if(useSearchBar) searchBarAction->setOn(true);
if(promptExit ) nAdvanced->setOn( true );
if(openDesktop) desktopAction->setOn( true );
if(filePerms) filePermAction->setOn( true );
if(startWithNew) nStart->setOn( true );
+ if(featureAutoSave) nAutoSave->setOn(true);
+
+// {
+// doTimer(true);
+// }
bool wrap = cfg. readBoolEntry ( "Wrap", true );
wa-> setOn ( wrap );
setWordWrap ( wrap );
+/////////////////
if( qApp->argc() > 1) {
currentFileName=qApp->argv()[1];
QFileInfo fi(currentFileName);
if(fi.baseName().left(1) == "") {
openDotFile(currentFileName);
} else {
openFile(currentFileName);
}
} else {
edited1=false;
openDotFile("");
}
viewSelection = cfg.readNumEntry( "FileView", 0 );
}
TextEdit::~TextEdit() {
qWarning("textedit d'tor");
delete editor;
}
void TextEdit::closeEvent(QCloseEvent *) {
if( edited1 && promptExit)
+ {
+ switch( savePrompt() )
+ {
+ case 1:
+ {
saveAs();
qApp->quit();
}
+ break;
+
+ case 2:
+ {
+ qApp->quit();
+ }
+ break;
+
+ case -1:
+ break;
+ };
+ }
+ else
+ qApp->quit();
+
+}
void TextEdit::cleanUp() {
Config cfg ( "TextEdit" );
cfg. setGroup ( "Font" );
QFont f = editor->font();
cfg.writeEntry ( "Family", f. family ( ));
cfg.writeEntry ( "Size", f. pointSize ( ));
cfg.writeEntry ( "Weight", f. weight ( ));
cfg.writeEntry ( "Italic", f. italic ( ));
cfg.setGroup ( "View" );
cfg.writeEntry ( "Wrap", editor->wordWrap() == QMultiLineEdit::WidgetWidth );
cfg.writeEntry ( "FileView", viewSelection );
cfg.writeEntry ( "PromptExit", promptExit );
cfg.writeEntry ( "OpenDesktop", openDesktop );
cfg.writeEntry ( "FilePermissions", filePerms );
cfg.writeEntry ( "SearchBar", useSearchBar );
cfg.writeEntry ( "startNew", startWithNew );
}
@@ -793,68 +830,70 @@ file.<BR>Open <B>.desktop</B> file or <B>linked</B> file?"),
}
void TextEdit::openFile( const DocLnk &f ) {
// clear();
// bFromDocView = true;
FileManager fm;
QString txt;
currentFileName=f.file();
qDebug("openFile doclnk " + currentFileName);
if ( !fm.loadFile( f, txt ) ) {
// ####### could be a new file
qDebug( "Cannot open file" );
}
// fileNew();
if ( doc )
delete doc;
doc = new DocLnk(f);
editor->setText(txt);
editor->setEdited( false);
edited1=false;
edited=false;
doc->setName(currentFileName);
updateCaption();
+ setTimer();
}
void TextEdit::showEditTools() {
menu->show();
editBar->show();
if(!useSearchBar)
searchBar->hide();
else
searchBar->show();
setWState (WState_Reserved1 );
}
/*!
unprompted save */
bool TextEdit::save() {
qDebug("saveAsFile " + currentFileName);
if(currentFileName.isEmpty()) {
saveAs();
return false;
}
+
QString file = doc->file();
qDebug("saver file "+file);
QString name= doc->name();
qDebug("File named "+name);
QString rt = editor->text();
if( !rt.isEmpty() ) {
if(name.isEmpty()) {
saveAs();
} else {
currentFileName= name ;
qDebug("saveFile "+currentFileName);
struct stat buf;
mode_t mode;
stat(file.latin1(), &buf);
mode = buf.st_mode;
if(!fileIs) {
doc->setName( name);
FileManager fm;
if ( !fm.saveFile( *doc, rt ) ) {
return false;
}
} else {
@@ -865,48 +904,49 @@ bool TextEdit::save() {
f.writeBlock(crt,crt.length());
} else {
QMessageBox::message(tr("Text Edit"),tr("Write Failed"));
return false;
}
}
editor->setEdited( false);
edited1=false;
edited=false;
if(caption().left(1)=="*")
setCaption(caption().right(caption().length()-1));
chmod( file.latin1(), mode);
}
return true;
}
return false;
}
/*!
prompted save */
bool TextEdit::saveAs() {
+
if(caption() == tr("Text Editor"))
return false;
qDebug("saveAsFile " + currentFileName);
// case of nothing to save...
// if ( !doc && !currentFileName.isEmpty()) {
// //|| !bFromDocView)
// qDebug("no doc");
// return true;
// }
// if ( !editor->edited() ) {
// delete doc;
// doc = 0;
// return true;
// }
QString rt = editor->text();
qDebug(currentFileName);
if( currentFileName.isEmpty()
|| currentFileName == tr("Unnamed")
|| currentFileName == tr("Text Editor")) {
qDebug("do silly TT filename thing");
// if ( doc && doc->name().isEmpty() ) {
QString pt = rt.simplifyWhiteSpace();
@@ -924,52 +964,57 @@ bool TextEdit::saveAs() {
docname = docname.left(40);
if ( docname.isEmpty() )
docname = tr("Unnamed");
if(doc) doc->setName(docname);
currentFileName=docname;
// }
// else
// qDebug("hmmmmmm");
}
QMap<QString, QStringList> map;
map.insert(tr("All"), QStringList() );
QStringList text;
text << "text/*";
map.insert(tr("Text"), text );
text << "*";
map.insert(tr("All"), text );
QFileInfo cuFi( currentFileName);
QString filee = cuFi.fileName();
QString dire = cuFi.dirPath();
if(dire==".")
dire = QPEApplication::documentDir();
- QString str = OFileDialog::getSaveFileName( 2,
+ QString str;
+ if( !featureAutoSave)
+ {
+ str = OFileDialog::getSaveFileName( 2,
dire,
filee, map);
-
+ }
+ else
+ str=currentFileName;
if(!str.isEmpty()) {
QString fileNm=str;
qDebug("saving filename "+fileNm);
QFileInfo fi(fileNm);
currentFileName=fi.fileName();
if(doc)
// QString file = doc->file();
// doc->removeFiles();
delete doc;
DocLnk nf;
nf.setType("text/plain");
nf.setFile( fileNm);
doc = new DocLnk(nf);
// editor->setText(rt);
qDebug("Saving file as "+currentFileName);
doc->setName( currentFileName);
updateCaption( currentFileName);
FileManager fm;
if ( !fm.saveFile( *doc, rt ) ) {
return false;
}
@@ -1119,24 +1164,102 @@ void TextEdit::doPrompt(bool b) {
}
void TextEdit::doDesktop(bool b) {
openDesktop=b;
Config cfg("TextEdit");
cfg.setGroup ( "View" );
cfg.writeEntry ( "OpenDesktop", b);
}
void TextEdit::doFilePerms(bool b) {
filePerms=b;
Config cfg("TextEdit");
cfg.setGroup ( "View" );
cfg.writeEntry ( "FilePermissions", b);
}
void TextEdit::editPasteTimeDate() {
#ifndef QT_NO_CLIPBOARD
QClipboard *cb = QApplication::clipboard();
QDateTime dt = QDateTime::currentDateTime();
cb->setText( dt.toString());
editor->paste();
#endif
}
+
+int TextEdit::savePrompt()
+{
+ switch( QMessageBox::information( 0, (tr("Textedit")),
+ (tr("Textedit detected\n"
+ "you have unsaved changes\n"
+ "Go ahead and save?\n")),
+ (tr("Save")), (tr("Don't Save")), (tr("&Cancel")), 2, 2 ) )
+ {
+ case 0:
+ {
+ return 1;
+ }
+ break;
+
+ case 1:
+ {
+ return 2;
+ }
+ break;
+
+ case 2:
+ {
+ return -1;
+ }
+ break;
+ };
+
+ return 0;
+}
+
+void TextEdit::timerCrank()
+{
+ if(featureAutoSave)
+ {
+ if( edited1 )
+ {
+ if(currentFileName.isEmpty())
+ {
+ currentFileName = QDir::homeDirPath()+"/textedit.tmp";
+ saveAs();
+ }
+ else
+ {
+ qDebug("autosave");
+ save();
+ }
+ setTimer();
+ }
+ }
+}
+
+void TextEdit::doTimer(bool b)
+{
+ Config cfg("TextEdit");
+ cfg.setGroup ( "View" );
+ cfg.writeEntry ( "autosave", b);
+ featureAutoSave = b;
+ nAutoSave->setOn(b);
+ if(b)
+ {
+ qDebug("doTimer true");
+ setTimer();
+ }
+ else
+ qDebug("doTimer false");
+}
+
+void TextEdit::setTimer()
+{
+if(featureAutoSave)
+ {
+ qDebug("setting autosave");
+ QTimer *timer = new QTimer(this );
+ connect( timer, SIGNAL(timeout()), this, SLOT(timerCrank()) );
+ timer->start( 30000/*0*/, true); //5 minutes
+ }
+}
diff --git a/core/apps/textedit/textedit.h b/core/apps/textedit/textedit.h
index 9f16ebf..643ea68 100644
--- a/core/apps/textedit/textedit.h
+++ b/core/apps/textedit/textedit.h
@@ -26,69 +26,75 @@
//#include "fileBrowser.h"
//#include "fileSaver.h"
#include <qpe/filemanager.h>
#include <qpe/qcopenvelope_qws.h>
#include <opie/ofileselector.h>
#include <qmainwindow.h>
#include <qmultilineedit.h>
#include <qlist.h>
#include <qmap.h>
class QAction;
class QWidgetStack;
class QToolButton;
class QPopupMenu;
class QToolBar;
class QLineEdit;
class QAction;
class FileSelector;
class QpeEditor;
class QPopupMenu;
+class QTimer;
class TextEdit : public QMainWindow
{
Q_OBJECT
public:
TextEdit( QWidget *parent = 0, const char *name = 0, WFlags f = 0 );
~TextEdit();
+protected:
QPopupMenu *font;
- QAction *nStart, *nFileDlgOpt, *nAdvanced, *desktopAction, *filePermAction, *searchBarAction;
+ QAction *nStart, *nFileDlgOpt, *nAdvanced, *desktopAction, *filePermAction, *searchBarAction, *nAutoSave;
bool edited, edited1;
void openFile( const QString & );
QCopChannel * channel;
-public slots:
- void editorChanged();
-void receive(const QCString&, const QByteArray&);
-protected:
+
+ bool featureAutoSave;
void closeEvent( QCloseEvent *e );
void doSearchBar();
+ int savePrompt();
+ void setTimer();
private slots:
+ void editorChanged();
+ void receive(const QCString&, const QByteArray&);
+ void timerCrank();
+ void doTimer(bool);
void editPasteTimeDate();
void doPrompt(bool);
void doDesktop(bool);
void doFilePerms(bool);
void doAbout();
void setDocument(const QString&);
void changeFont();
void fileNew();
void fileRevert();
void fileOpen();
void changeStartConfig(bool);
bool save();
bool saveAs();
void cleanUp();
void editCut();
void editCopy();
void editPaste();
void editFind();
void editDelete();
void findNext();
void findClose();