aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coreplugin/dialogs/filepropertiesdialog.cpp
blob: e4ed48648bf1c389760db8ac87da664cc1b4d34d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/****************************************************************************
**
** Copyright (C) 2018 Andre Hartmann <aha_1980@gmx.de>
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "filepropertiesdialog.h"
#include "ui_filepropertiesdialog.h"

#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditorfactory.h>
#include <utils/fileutils.h>
#include <utils/mimetypes/mimedatabase.h>

#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QLocale>

FilePropertiesDialog::FilePropertiesDialog(const Utils::FileName &fileName, QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::FilePropertiesDialog),
    m_fileName(fileName.toString())
{
    m_ui->setupUi(this);

    connect(m_ui->readable, &QCheckBox::clicked, [this](bool checked) {
        setPermission(QFile::ReadUser | QFile::ReadOwner, checked);
    });
    connect(m_ui->writable, &QCheckBox::clicked, [this](bool checked) {
        setPermission(QFile::WriteUser | QFile::WriteOwner, checked);
    });
    connect(m_ui->executable, &QCheckBox::clicked, [this](bool checked) {
        setPermission(QFile::ExeUser | QFile::ExeOwner, checked);
    });

    refresh();
}

FilePropertiesDialog::~FilePropertiesDialog()
{
    delete m_ui;
}

void FilePropertiesDialog::refresh()
{
    Utils::withNtfsPermissions<void>([this] {
        const QFileInfo fileInfo(m_fileName);
        QLocale locale;

        m_ui->name->setText(fileInfo.fileName());
        m_ui->path->setText(QDir::toNativeSeparators(fileInfo.canonicalPath()));

        m_ui->mimeType->setText(Utils::mimeTypeForFile(fileInfo).name());

        const Core::EditorFactoryList factories = Core::IEditorFactory::preferredEditorFactories(m_fileName);
        m_ui->defaultEditor->setText(!factories.isEmpty() ? factories.at(0)->displayName() : tr("Undefined"));

        m_ui->owner->setText(fileInfo.owner());
        m_ui->group->setText(fileInfo.group());
        m_ui->size->setText(locale.formattedDataSize(fileInfo.size()));
        m_ui->readable->setChecked(fileInfo.isReadable());
        m_ui->writable->setChecked(fileInfo.isWritable());
        m_ui->executable->setChecked(fileInfo.isExecutable());
        m_ui->symLink->setChecked(fileInfo.isSymLink());
        m_ui->lastRead->setText(fileInfo.lastRead().toString(locale.dateTimeFormat()));
        m_ui->lastModified->setText(fileInfo.lastModified().toString(locale.dateTimeFormat()));
    });
}

void FilePropertiesDialog::setPermission(QFile::Permissions newPermissions, bool set)
{
    Utils::withNtfsPermissions<void>([this, newPermissions, set] {
        QFile::Permissions permissions = QFile::permissions(m_fileName);
        if (set)
            permissions |= newPermissions;
        else
            permissions &= ~newPermissions;

        if (!QFile::setPermissions(m_fileName, permissions))
            qWarning() << "Cannot change permissions for file" << m_fileName;
    });

    refresh();
}