<commit_before>
#include <QApplication>
#include <QTextEdit>
#include <QtGui>
#include <QTextCodec>
#include "mainwindow.h"
#include "login.h"
#include "desktopimage.h"

#include <fcntl.h>
#include <framework/mod/options.h>
#include <module/embox/arch/x86/boot/multiboot.h>

#define MBOOTMOD embox__arch__x86__boot__multiboot

#define WIDTH  OPTION_MODULE_GET(MBOOTMOD,NUMBER,video_width)
#define HEIGHT OPTION_MODULE_GET(MBOOTMOD,NUMBER,video_height)
#define BPP    OPTION_MODULE_GET(MBOOTMOD,NUMBER,video_depth)

static QList<TextEditor*> textEditors;
TextEditor *textEditor;
QMdiArea *emarea;
QMdiSubWindow *emEditorSubWindow;
static DesktopImageDialog *wallpaperDialog;
static QStringList desktopImagesList;

void textEditorClosed(TextEditor *ed) {
	for (int i = 0; i < textEditors.size(); i++) {
		if (textEditors.at(i) == ed) {
			textEditors.removeAt(i);
		}
	}
}

static void emboxShowLoginForm();

static QApplication *__qt_app;
static QMenu *__qt_menu;

class EmboxRootWindow : public QMainWindow
{
    Q_OBJECT

    public:

    QAction *closeAllEditorsAction;
    EmboxRootWindow() {
    	fileMenu = new QMenu(QString("&Пуск"), this);
    	menuBar()->addMenu(fileMenu);

	__qt_menu = fileMenu;

    	textEditorAction = new QAction(QString("&Текстовый редактор"), this);
    	fileMenu->addAction(textEditorAction);
    	connect(textEditorAction, SIGNAL(triggered()), this, SLOT(textEditorRun()));

    	closeAllEditorsAction = new QAction(QString("&Закрыть все окна"), this);
    	fileMenu->addAction(closeAllEditorsAction);
    	connect(closeAllEditorsAction, SIGNAL(triggered()), this, SLOT(closeAllEditors()));

    	logoutAction = new QAction(QString("&Завершение сеанса"), this);
    	fileMenu->addAction(logoutAction);
    	connect(logoutAction, SIGNAL(triggered()), this, SLOT(logout()));

    	saveAction = new QAction(QString("&Сохранить конфигурацию"), this);
    	fileMenu->addAction(saveAction);
    	connect(saveAction, SIGNAL(triggered()), this, SLOT(savedefault()));

    	wallpaperAction = new QAction(QString("&Графические настройки"), this);
    	fileMenu->addAction(wallpaperAction);
    	connect(wallpaperAction, SIGNAL(triggered()), this, SLOT(wallpaper()));
    }

    private slots:
    	void textEditorRun() {
    		textEditor = new TextEditor();
		textEditors << textEditor;
    		emEditorSubWindow = emarea->addSubWindow(textEditor, textEditor->windowType());
		textEditor->subwindow = emEditorSubWindow;
    		textEditor->show();
    	}

    	void closeAllEditors() {
		for (int i = 0; i < textEditors.size(); i++) {
			textEditors.at(i)->subwindow->close();
		}
	}

    	void logout() {
    		emboxHideDesktop();
    		emboxShowLoginForm();
    	}

    	void wallpaper() {
    		wallpaperDialog = new DesktopImageDialog(desktopImagesList);
    		wallpaperDialog->show();
    	}

    	void savedefault() {

    	}

    private:
    	QAction *logoutAction;
        QAction *textEditorAction;
        QAction *wallpaperAction;
        QAction *saveAction;
        QMenu *fileMenu;
};

EmboxRootWindow *emroot;

#define FSIZE 64

static int curuid;

#define SEP ':'

static char *parse(char **buf) {
	char *ret = *buf;
	char *ch;

	if ((ch = strchr(*buf, SEP))) {
		*ch++ = '\0';
	}

	*buf = ch;

	return ret;
}

void qtSetfont(const QString &fontFamily, int fontPt) {
	QFont serifFont(fontFamily, fontPt);
	__qt_app->setFont(serifFont);
	__qt_menu->setFont(serifFont);
}

static void load_pref(void) {
	int fd, ret;
	char cbuf[FSIZE];
	const char *wall = "default.png";
	const char *font = "Times";
	int font_pt = 10;

	snprintf(cbuf, FSIZE, "/mnt/pref_%d", curuid);
	fd = open(cbuf, O_RDONLY, 0755);

	lseek(fd, 0, 0);
	if (0 < (ret = read(fd, cbuf, FSIZE))) {
		char *buf = cbuf;

		buf[ret] = '\0';

		wall = parse(&buf);
		font = parse(&buf);
		font_pt = atoi(parse(&buf));
	}
	close(fd);

	QString imagePath = QString(":/images/").append(QString(wall));
	QImage desktopImage = QImage(imagePath).convertToFormat(QImage::Format_RGB16).scaled(WIDTH, HEIGHT, Qt::KeepAspectRatio);
	QPixmap bgPix = QPixmap::fromImage(desktopImage);
	emarea->setBackground(bgPix);

	qtSetfont(font, font_pt);

}

void save_pref(char *wall, char *font, int font_pt) {
	int fd, len;
	char cbuf[FSIZE];

	snprintf(cbuf, FSIZE, "/mnt/pref_%d", curuid);

	fd = creat(cbuf, 0755);

	len = snprintf(cbuf, FSIZE, "%s:%s:%d", wall, font, font_pt);

	write(fd, cbuf, len);

	ftruncate(fd, len);

	close(fd);
}

void emboxShowDesktop(uid_t uid) {
	int fd = -1;
	bool enabled = uid ? false : true;

	curuid = uid;
	emroot->menuBar()->show();
	emroot->closeAllEditorsAction->setEnabled(enabled);

	load_pref();
}

void emboxHideDesktop() {
	emarea->closeAllSubWindows();
	emroot->menuBar()->hide();
}

static void emboxShowLoginForm() {
    LoginDialog *loginDialog = new LoginDialog();<commit_msg>qt: sync after saving desktop settings<commit_after>
#include <QApplication>
#include <QTextEdit>
#include <QtGui>
#include <QTextCodec>
#include "mainwindow.h"
#include "login.h"
#include "desktopimage.h"

#include <fcntl.h>
#include <framework/mod/options.h>
#include <module/embox/arch/x86/boot/multiboot.h>

#define MBOOTMOD embox__arch__x86__boot__multiboot

#define WIDTH  OPTION_MODULE_GET(MBOOTMOD,NUMBER,video_width)
#define HEIGHT OPTION_MODULE_GET(MBOOTMOD,NUMBER,video_height)
#define BPP    OPTION_MODULE_GET(MBOOTMOD,NUMBER,video_depth)

static QList<TextEditor*> textEditors;
TextEditor *textEditor;
QMdiArea *emarea;
QMdiSubWindow *emEditorSubWindow;
static DesktopImageDialog *wallpaperDialog;
static QStringList desktopImagesList;

void textEditorClosed(TextEditor *ed) {
	for (int i = 0; i < textEditors.size(); i++) {
		if (textEditors.at(i) == ed) {
			textEditors.removeAt(i);
		}
	}
}

static void emboxShowLoginForm();

static QApplication *__qt_app;
static QMenu *__qt_menu;

class EmboxRootWindow : public QMainWindow
{
    Q_OBJECT

    public:

    QAction *closeAllEditorsAction;
    EmboxRootWindow() {
    	fileMenu = new QMenu(QString("&Пуск"), this);
    	menuBar()->addMenu(fileMenu);

	__qt_menu = fileMenu;

    	textEditorAction = new QAction(QString("&Текстовый редактор"), this);
    	fileMenu->addAction(textEditorAction);
    	connect(textEditorAction, SIGNAL(triggered()), this, SLOT(textEditorRun()));

    	closeAllEditorsAction = new QAction(QString("&Закрыть все окна"), this);
    	fileMenu->addAction(closeAllEditorsAction);
    	connect(closeAllEditorsAction, SIGNAL(triggered()), this, SLOT(closeAllEditors()));

    	logoutAction = new QAction(QString("&Завершение сеанса"), this);
    	fileMenu->addAction(logoutAction);
    	connect(logoutAction, SIGNAL(triggered()), this, SLOT(logout()));

    	saveAction = new QAction(QString("&Сохранить конфигурацию"), this);
    	fileMenu->addAction(saveAction);
    	connect(saveAction, SIGNAL(triggered()), this, SLOT(savedefault()));

    	wallpaperAction = new QAction(QString("&Графические настройки"), this);
    	fileMenu->addAction(wallpaperAction);
    	connect(wallpaperAction, SIGNAL(triggered()), this, SLOT(wallpaper()));
    }

    private slots:
    	void textEditorRun() {
    		textEditor = new TextEditor();
		textEditors << textEditor;
    		emEditorSubWindow = emarea->addSubWindow(textEditor, textEditor->windowType());
		textEditor->subwindow = emEditorSubWindow;
    		textEditor->show();
    	}

    	void closeAllEditors() {
		for (int i = 0; i < textEditors.size(); i++) {
			textEditors.at(i)->subwindow->close();
		}
	}

    	void logout() {
    		emboxHideDesktop();
    		emboxShowLoginForm();
    	}

    	void wallpaper() {
    		wallpaperDialog = new DesktopImageDialog(desktopImagesList);
    		wallpaperDialog->show();
    	}

    	void savedefault() {

    	}

    private:
    	QAction *logoutAction;
        QAction *textEditorAction;
        QAction *wallpaperAction;
        QAction *saveAction;
        QMenu *fileMenu;
};

EmboxRootWindow *emroot;

#define FSIZE 64

static int curuid;

#define SEP ':'

static char *parse(char **buf) {
	char *ret = *buf;
	char *ch;

	if ((ch = strchr(*buf, SEP))) {
		*ch++ = '\0';
	}

	*buf = ch;

	return ret;
}

void qtSetfont(const QString &fontFamily, int fontPt) {
	QFont serifFont(fontFamily, fontPt);
	__qt_app->setFont(serifFont);
	__qt_menu->setFont(serifFont);
}

static void load_pref(void) {
	int fd, ret;
	char cbuf[FSIZE];
	const char *wall = "default.png";
	const char *font = "Times";
	int font_pt = 10;

	snprintf(cbuf, FSIZE, "/mnt/pref_%d", curuid);
	fd = open(cbuf, O_RDONLY, 0755);

	lseek(fd, 0, 0);
	if (0 < (ret = read(fd, cbuf, FSIZE))) {
		char *buf = cbuf;

		buf[ret] = '\0';

		wall = parse(&buf);
		font = parse(&buf);
		font_pt = atoi(parse(&buf));
	}
	close(fd);

	QString imagePath = QString(":/images/").append(QString(wall));
	QImage desktopImage = QImage(imagePath).convertToFormat(QImage::Format_RGB16).scaled(WIDTH, HEIGHT, Qt::KeepAspectRatio);
	QPixmap bgPix = QPixmap::fromImage(desktopImage);
	emarea->setBackground(bgPix);

	qtSetfont(font, font_pt);

}

void save_pref(char *wall, char *font, int font_pt) {
	int fd, len;
	char cbuf[FSIZE];

	snprintf(cbuf, FSIZE, "/mnt/pref_%d", curuid);

	fd = creat(cbuf, 0755);

	len = snprintf(cbuf, FSIZE, "%s:%s:%d", wall, font, font_pt);

	write(fd, cbuf, len);

	ftruncate(fd, len);

	/*
	 *  XXX add sync() in this place is bad idea because of performance,
	 *  but we don't have better mechanism as fsync() or synchronization before shutdown.
	 *  So, simply force sync() here. --Alexander
	 */
	sync();

	close(fd);
}

void emboxShowDesktop(uid_t uid) {
	int fd = -1;
	bool enabled = uid ? false : true;

	curuid = uid;
	emroot->menuBar()->show();
	emroot->closeAllEditorsAction->setEnabled(enabled);

	load_pref();
}

void emboxHideDesktop() {
	emarea->closeAllSubWindows();
	emroot->menuBar()->hide();
}

static void emboxShowLoginForm() {
    LoginDialog *loginDialog = new LoginDialog();
{'fraction_non_alphanumeric': 0.1434659090909091, 'fraction_numerical': 0.005275974025974026, 'mean_word_length': 4.556369785794814, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '517757', 'n_tokens_mistral': 3728, 'n_tokens_neox': 3570, 'n_words': 729}