PERFORCE change 166633 for review

Jonathan Anderson jona at FreeBSD.org
Mon Jul 27 16:42:57 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=166633

Change 166633 by jona at jona-trustedbsd-belle-vmware on 2009/07/27 16:42:02

	Added close action, 'current file'

Affected files ...

.. //depot/projects/trustedbsd/capabilities/src/tools/cap/sandbox_qt/TextEditor.cpp#3 edit
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/sandbox_qt/TextEditor.h#2 edit

Differences ...

==== //depot/projects/trustedbsd/capabilities/src/tools/cap/sandbox_qt/TextEditor.cpp#3 (text+ko) ====

@@ -44,27 +44,32 @@
 
 
 TextEditor::TextEditor()
-	: QMainWindow()
+	: QMainWindow(), currentFile(this)
 {
 	setAttribute(Qt::WA_QuitOnClose);
 	setWindowTitle(tr("Sandboxed Text Editor"));
 
-	openFile = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
+	openFile = new QAction(QIcon(":/icons/open.png"), tr("&Open..."), this);
 	openFile->setShortcuts(QKeySequence::Open);
 	openFile->setStatusTip(tr("Open an existing file"));
 	connect(openFile, SIGNAL(triggered()), this, SLOT(open()));
 
-	saveFile = new QAction(QIcon(":/images/save.png"), tr("&Save..."), this);
+	saveFile = new QAction(QIcon(":/icons/save.png"), tr("&Save..."), this);
 	saveFile->setShortcuts(QKeySequence::Save);
 	saveFile->setStatusTip(tr("Save the file"));
 	connect(saveFile, SIGNAL(triggered()), this, SLOT(save()));
 
-	openFileRaw = new QAction(QIcon(":/images/open.png"),
+	openFileRaw = new QAction(QIcon(":/icons/open.png"),
 	                               tr("&Open via raw syscalls..."), this);
 	openFileRaw->setStatusTip(tr("Open a file via raw system calls"));
 	connect(openFileRaw, SIGNAL(triggered()), this, SLOT(openRaw()));
 
-	quitEditor = new QAction(QIcon(":/images/quit.png"), tr("&Quit"), this);
+	closeFile = new QAction(QIcon(":/icons/close.png"), tr("&Close..."), this);
+	closeFile->setShortcuts(QKeySequence::Close);
+	closeFile->setStatusTip(tr("Close the current file"));
+	connect(closeFile, SIGNAL(triggered()), this, SLOT(close()));
+
+	quitEditor = new QAction(QIcon(":/icons/quit.png"), tr("&Quit"), this);
 	quitEditor->setShortcut(QKeySequence("Ctrl+Q"));
 	quitEditor->setStatusTip(tr("Quit Text Editor"));
 	connect(quitEditor, SIGNAL(triggered()), this, SLOT(quit()));
@@ -73,14 +78,16 @@
 	fileMenu->addAction(openFile);
 	fileMenu->addAction(openFileRaw);
 	fileMenu->addAction(saveFile);
+	fileMenu->addAction(closeFile);
 	fileMenu->addAction(quitEditor);
-/*
+
 	fileToolBar = addToolBar("File");
 	fileToolBar->addAction(openFile);
 	fileToolBar->addAction(openFileRaw);
 	fileToolBar->addAction(saveFile);
+	fileToolBar->addAction(closeFile);
 	fileToolBar->addAction(quitEditor);
-*/
+
 	setCentralWidget(text = new QTextEdit());
 
 	resize(640, 480);
@@ -88,6 +95,12 @@
 }
 
 
+TextEditor::~TextEditor()
+{
+	close();
+}
+
+
 void TextEditor::open()
 {
 	struct ua_powerbox_options options;
@@ -108,58 +121,56 @@
 	char *name;
 
 	if(ua_powerbox(&options, &fd, &name, &fdcount))
+	{
 		QMessageBox::critical(this,
 		                      tr("Powerbox Error"),
 		                      tr("Error opening User Angel powerbox: ")
 		                       + strerror(errno));
+		return;
+	}
 
-	else
-	{
-		QFile file(this);
-		FILE *fp = fdopen(fd, "r");
-		file.open(fp, QFile::ReadOnly);
-		text->setPlainText(file.readAll());
-		file.close();
-		fclose(fp);
-	}
+	currentFile.open(fdopen(fd, "rw"), QFile::ReadWrite);
+	text->setPlainText(currentFile.readAll());
 }
 
 
 void TextEditor::save()
 {
-	struct ua_powerbox_options options;
-	options.ui              = UA_QT;
-	options.operation       = UA_SAVE_FILE;
-	options.parent_window   = this->winId();
-	options.start_path      = "";
-	options.pathlen         = 0;
-	options.start_fd        = -1;
-	options.mult            = false;
-	options.filter          = "";
-	options.filterlen       = 0;
-	options.flags           = O_WRONLY | O_CREAT | O_TRUNC;
-	options.rights          = CAP_FSTAT | CAP_SEEK | CAP_FSYNC | CAP_WRITE | CAP_FTRUNCATE;
-	options.umask		= 0666;
+	if(!currentFile.isOpen())
+	{
+		struct ua_powerbox_options options;
+		options.ui              = UA_QT;
+		options.operation       = UA_SAVE_FILE;
+		options.parent_window   = this->winId();
+		options.start_path      = "";
+		options.pathlen         = 0;
+		options.start_fd        = -1;
+		options.mult            = false;
+		options.filter          = "";
+		options.filterlen       = 0;
+		options.flags           = O_WRONLY | O_CREAT | O_TRUNC;
+		options.rights          = CAP_FSTAT | CAP_SEEK | CAP_FSYNC | CAP_READ | CAP_WRITE | CAP_FTRUNCATE;
+		options.umask		= 0666;
 
-	int fdcount = 1;
-	int fd;
-	char *name;
+		int fdcount = 1;
+		int fd;
+		char *name;
 
-	if(ua_powerbox(&options, &fd, &name, &fdcount))
-		QMessageBox::critical(this,
-		                      tr("Powerbox Error"),
-		                      tr("Error opening User Angel powerbox: ")
-		                       + strerror(errno));
+		if(ua_powerbox(&options, &fd, &name, &fdcount))
+		{
+			QMessageBox::critical(this,
+			                      tr("Powerbox Error"),
+			                      tr("Error opening User Angel powerbox: ")
+			                       + strerror(errno));
+			return;
+		}
 
-	else
-	{
-		QFile file(this);
-		file.open(fd, QFile::WriteOnly | QFile::Truncate);
-		int ret = file.write(text->toPlainText().toAscii());
-		file.flush();
-		file.close();
-		::close(fd);
+		currentFile.open(fd, QFile::ReadWrite);
 	}
+
+	currentFile.resize(0);
+	currentFile.write(text->toPlainText().toAscii());
+	currentFile.flush();
 }
 
 
@@ -174,8 +185,16 @@
 }
 
 
+void TextEditor::close()
+{
+	currentFile.close();
+	text->setPlainText("");
+}
+
+
 void TextEditor::quit()
 {
 	close();
+	QMainWindow::close();
 }
 

==== //depot/projects/trustedbsd/capabilities/src/tools/cap/sandbox_qt/TextEditor.h#2 (text+ko) ====

@@ -31,6 +31,7 @@
  * SUCH DAMAGE.
  */
 
+#include <QFile>
 #include <QMainWindow>
 
 class QAction;
@@ -45,12 +46,14 @@
 
 	public:
 	TextEditor();
+	~TextEditor();
 
 
 	public slots:
 	void open();
 	void save();
 	void openRaw();
+	void close();
 	void quit();
 
 
@@ -63,6 +66,9 @@
 	QAction *openFile;
 	QAction *saveFile;
 	QAction *openFileRaw;
+	QAction *closeFile;
 	QAction *quitEditor;
+
+	QFile currentFile;
 };
 


More information about the p4-projects mailing list