aboutsummaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorIgor <pashev.igor@gmail.com>2010-11-18 20:09:43 +0300
committerIgor <pashev.igor@gmail.com>2010-11-18 20:09:43 +0300
commit157370c4afbeb49836e10bff0a3400e12fd34f94 (patch)
tree69cdb0e0926d4efc1b8742ca650056ce84084d98 /cpp
parent0ea94d3ba924eaf5b739ab4cf4d30b39631476a0 (diff)
downloadnewslack-157370c4afbeb49836e10bff0a3400e12fd34f94.tar.gz
C++
Diffstat (limited to 'cpp')
-rw-r--r--cpp/main.cpp18
-rw-r--r--cpp/mainwindow.cpp220
-rw-r--r--cpp/mainwindow.h40
-rw-r--r--cpp/mainwindow.ui113
-rw-r--r--cpp/newslack.pro18
5 files changed, 409 insertions, 0 deletions
diff --git a/cpp/main.cpp b/cpp/main.cpp
new file mode 100644
index 0000000..db7ef9d
--- /dev/null
+++ b/cpp/main.cpp
@@ -0,0 +1,18 @@
+#include <QtGui/QApplication>
+#include "mainwindow.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ MainWindow w;
+
+ if (argc > 1)
+ for (int i = 1; i < argc; i++)
+ w.findFiles(argv[i]);
+ else
+ w.findFiles("/home/pashev/tmp");
+
+ w.show();
+
+ return a.exec();
+}
diff --git a/cpp/mainwindow.cpp b/cpp/mainwindow.cpp
new file mode 100644
index 0000000..4ad16f0
--- /dev/null
+++ b/cpp/mainwindow.cpp
@@ -0,0 +1,220 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+#include <QDir>
+#include <QDebug>
+#include <QProcess>
+#include <QRegExp>
+#include <QTextBlock>
+
+MainWindow::MainWindow(QWidget *parent) :
+ QMainWindow(parent),
+ ui(new Ui::MainWindow)
+{
+ ui->setupUi(this);
+}
+
+MainWindow::~MainWindow()
+{
+ delete ui;
+}
+
+void MainWindow::changeEvent(QEvent *e)
+{
+ QMainWindow::changeEvent(e);
+ switch (e->type()) {
+ case QEvent::LanguageChange:
+ ui->retranslateUi(this);
+ break;
+ default:
+ break;
+ }
+}
+
+void MainWindow::on_quit_clicked()
+{
+ qApp->quit();
+}
+
+void MainWindow::findFiles(QString path)
+{
+ QDir currentDir = QDir(path);
+ QStringList files =
+ currentDir.entryList(QStringList("*.new"),
+ QDir::Files | QDir::NoSymLinks);
+ Q_FOREACH (QString f, files)
+ {
+ ui->files->addItem(path + "/" + f);
+ }
+
+ files =
+ currentDir.entryList(QStringList("*"),
+ QDir::Dirs | QDir::NoSymLinks
+ | QDir::NoDot | QDir::NoDotDot);
+ Q_FOREACH (QString dir, files)
+ {
+ findFiles(path + "/" + dir);
+ }
+}
+
+void MainWindow::enableButtons()
+{
+ bool enable = 0 != ui->files->count();
+ ui->accept->setEnabled(enable);
+ ui->reject->setEnabled(enable);
+ ui->edit->setEnabled(enable);
+}
+
+void MainWindow::on_files_currentItemChanged
+ (QListWidgetItem* current, QListWidgetItem* /*previous*/)
+{
+ if (!current)
+ {
+ ui->diff->clear();
+ return;
+ }
+ update_diff();
+}
+
+void MainWindow::update_diff()
+{
+ QString fold, fnew;
+ fold = fnew = ui->files->currentItem()->text();
+ fold.replace(QRegExp("\\.new$"), "");
+ QStringList difflines = diff(fold, fnew).split("\n");
+ difflines.replaceInStrings(QRegExp("(@@[^@]+@@)"),
+ "<span style=\"color:blue;font-weight:bold;\">\\1</span>");
+ difflines.replaceInStrings(QRegExp("^((---|\\+\\+\\+)\\s.*)$"),
+ "<span style=\"font-weight:bold;\">\\1</span>");
+ difflines.replaceInStrings(QRegExp("^(-.*)$"),
+ "<span style=\"color:red;font-weight:bold;\">\\1</span>");
+ difflines.replaceInStrings(QRegExp("^(\\+.*)$"),
+ "<span style=\"color:green;font-weight:bold;\">\\1</span>");
+
+ if (difflines.count() < 2)
+ on_reject_clicked();
+ else
+ {
+ ui->diff->setHtml("<pre>" + difflines.join("\n") + "</pre>");
+ enableButtons();
+ }
+}
+
+QString MainWindow::diff(QString fold, QString fnew)
+{
+ QProcess pipe;
+ QStringList args;
+ args << "-udb" << fold << fnew;
+ pipe.start("diff", args, QIODevice::ReadOnly);
+ pipe.waitForFinished();
+ QString res = QString::fromUtf8(
+ pipe.readAllStandardOutput().constData());
+ return res;
+}
+
+void MainWindow::on_accept_clicked()
+{
+ QString fold, fnew;
+ fold = fnew = ui->files->currentItem()->text();
+ fold.replace(QRegExp("\\.new$"), "");
+ if (QFile::remove(fold) && QFile::rename(fnew, fold))
+ {
+ ui->files->takeItem(
+ ui->files->row(
+ ui->files->currentItem()));
+ }
+ else
+ {
+ qWarning() << "Unable to move"
+ << fnew << "to" << fold;
+ }
+ enableButtons();
+}
+
+void MainWindow::on_reject_clicked()
+{
+ QString fnew;
+ fnew = ui->files->currentItem()->text();
+ if (QFile::remove(fnew))
+ {
+ ui->files->takeItem(ui->files->row(ui->files->currentItem()));
+ }
+ else
+ {
+ qWarning() << "Unable to delete" << fnew;
+ }
+ enableButtons();
+}
+
+void MainWindow::on_diff_selectionChanged()
+{
+ ui->apply->setEnabled(
+ ui->diff->textCursor().selectionStart() !=
+ ui->diff->textCursor().selectionEnd());
+}
+
+void MainWindow::on_edit_clicked()
+{
+ QString fold, fnew;
+ fold = fnew = ui->files->currentItem()->text();
+ fold.replace(QRegExp("\\.new$"), "");
+
+ QProcess editor;
+ QStringList args;
+ args << "-f";
+ args << fold;
+ editor.start("gvim", args);
+ editor.waitForFinished();
+ update_diff();
+}
+
+void MainWindow::on_apply_clicked()
+{
+ int start = ui->diff->textCursor().selectionStart();
+ int end = ui->diff->textCursor().selectionEnd();
+
+ int first = ui->diff->document()->findBlock(start).firstLineNumber();
+ int last = ui->diff->document()->findBlock(end).firstLineNumber();
+
+ if (last <= first)
+ last = first + 1;
+
+ QStringList lines = ui->diff->toPlainText().split("\n");
+
+ QStringList part_diff;
+ part_diff << lines[0] << lines[1];
+
+ // Looking for the begining
+ if (first < 2) first = 2;
+ else
+ while (lines[first].left(2) != "@@")
+ first--;
+
+ // Looking for the end
+ int nlines = lines.count();
+ while (last < nlines)
+ {
+ if (lines[last].left(2) == "@@")
+ break;
+ else last++;
+ }
+ part_diff << lines.mid(first, last-first) << "\n";
+ patch(part_diff.join("\n"));
+ update_diff();
+}
+
+void MainWindow::patch(QString diff)
+{
+ QProcess pipe;
+ QStringList args;
+ // args << "--dry-run"; // TODO: remove me
+ args << "-p0" << "-l";
+ pipe.start("patch", args);
+ pipe.waitForStarted();
+ pipe.write(diff.toUtf8());
+ pipe.closeWriteChannel();
+ pipe.waitForFinished();
+ qDebug() << pipe.readAllStandardOutput();
+}
+
+
+
diff --git a/cpp/mainwindow.h b/cpp/mainwindow.h
new file mode 100644
index 0000000..f18ee95
--- /dev/null
+++ b/cpp/mainwindow.h
@@ -0,0 +1,40 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QtGui/QMainWindow>
+#include <QListWidgetItem>
+
+namespace Ui {
+ class MainWindow;
+}
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ explicit MainWindow(QWidget *parent = 0);
+ void findFiles(QString);
+ ~MainWindow();
+
+protected:
+ void changeEvent(QEvent *e);
+ void patch(QString);
+ QString diff(QString, QString);
+ void enableButtons();
+ void update_diff();
+
+private:
+ Ui::MainWindow *ui;
+
+private slots:
+ void on_apply_clicked();
+ void on_edit_clicked();
+ void on_diff_selectionChanged();
+ void on_reject_clicked();
+ void on_accept_clicked();
+ void on_files_currentItemChanged(QListWidgetItem* current, QListWidgetItem* previous);
+ void on_quit_clicked();
+};
+
+#endif // MAINWINDOW_H
diff --git a/cpp/mainwindow.ui b/cpp/mainwindow.ui
new file mode 100644
index 0000000..93f7ac2
--- /dev/null
+++ b/cpp/mainwindow.ui
@@ -0,0 +1,113 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>600</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>MainWindow</string>
+ </property>
+ <widget class="QWidget" name="centralWidget">
+ <layout class="QGridLayout" name="gridLayout">
+ <item row="0" column="0">
+ <widget class="QSplitter" name="splitter">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <widget class="QWidget" name="verticalLayoutWidget_2">
+ <layout class="QVBoxLayout" name="verticalLayout_2">
+ <item>
+ <widget class="QTextEdit" name="diff">
+ <property name="font">
+ <font>
+ <family>Monospace</family>
+ <pointsize>12</pointsize>
+ </font>
+ </property>
+ <property name="readOnly">
+ <bool>true</bool>
+ </property>
+ <property name="tabStopWidth">
+ <number>40</number>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QVBoxLayout" name="verticalLayout_3">
+ <item>
+ <widget class="QPushButton" name="apply">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="text">
+ <string>A&amp;pply selection</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QPushButton" name="accept">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="text">
+ <string>&amp;Accept</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="reject">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="text">
+ <string>&amp;Reject</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="edit">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="text">
+ <string>&amp;Edit old...</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <widget class="QWidget" name="verticalLayoutWidget">
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="QListWidget" name="files"/>
+ </item>
+ <item>
+ <widget class="QPushButton" name="quit">
+ <property name="text">
+ <string>&amp;Quit</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/cpp/newslack.pro b/cpp/newslack.pro
new file mode 100644
index 0000000..df4fb8b
--- /dev/null
+++ b/cpp/newslack.pro
@@ -0,0 +1,18 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2010-11-06T17:12:39
+#
+#-------------------------------------------------
+
+QT += core gui
+
+TARGET = newslack
+TEMPLATE = app
+
+
+SOURCES += main.cpp\
+ mainwindow.cpp
+
+HEADERS += mainwindow.h
+
+FORMS += mainwindow.ui