aboutsummaryrefslogtreecommitdiff
path: root/src/qmmpui/winfileassocpage.cpp
blob: a409472fc7b7693d6c75e56ab236c023f65fb790 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***************************************************************************
 * Based on smplayer - GUI front-end for mplayer                           *
 *                                                                         *
 * Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>    *
 * Copyright (C) 2014 Ilya Kotov forkotov02@hotmail.ru                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include <QSettings>
#include <QApplication>
#include <QMessageBox>
#include <qmmp/metadatamanager.h>
#include "ui_winfileassocpage.h"
#include "winfileassocpage_p.h"
#include "winfileassoc.h"

WinFileAssocPage::WinFileAssocPage(QWidget *parent): QWidget(parent)
{
    m_ui = new Ui::WinFileAssocPage;
    m_ui->setupUi(this);

    if (QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA)
	{
		//Hide Select None - One cannot restore an association in Vista. Go figure.
        m_ui->selectNone->hide();
    }

    foreach (QString ext, MetaDataManager::instance()->nameFilters())
    {
        ext.remove("*.");
        m_ui->listWidget->addItem(ext);
        m_extensions.append(ext);
    }
    loadAssociations();
    createHelp();
}

WinFileAssocPage::~WinFileAssocPage()
{
    saveAssociations();
}

void WinFileAssocPage::on_selectAll_clicked()
{
    for (int k = 0; k < m_ui->listWidget->count(); k++)
        m_ui->listWidget->item(k)->setCheckState(Qt::Checked);
    m_ui->listWidget->setFocus();
}

void WinFileAssocPage::on_selectNone_clicked()
{
    for (int k = 0; k < m_ui->listWidget->count(); k++)
        m_ui->listWidget->item(k)->setCheckState(Qt::Unchecked);
    m_ui->listWidget->setFocus();
}

void WinFileAssocPage::loadAssociations()
{
	m_regExtensions.clear(); 
    WinFileAssoc ().GetRegisteredExtensions(m_extensions, m_regExtensions);

    for (int k = 0; k < m_ui->listWidget->count(); k++)
	{
        QListWidgetItem* pItem = m_ui->listWidget->item(k);
		if (pItem)
		{
			pItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);

            if (m_regExtensions.contains(pItem->text()))
			{
				pItem->setCheckState(Qt::Checked);
				//Don't allow de-selection in windows VISTA if extension is registered.
				//VISTA doesn't seem to support extension 'restoration' in the API.
                if (QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA)
                {
					pItem->setFlags(0);
				}
			}
			else
			{
				pItem->setCheckState(Qt::Unchecked);
			}
		}
	}
}

int WinFileAssocPage::ProcessAssociations(QStringList& current, QStringList& old)
{
	WinFileAssoc RegAssoc; 

	QStringList toRestore; 

	//Restore unselected associations
	foreach(const QString& ext, old)
	{
		if (!current.contains(ext))
			toRestore.append(ext); 
	}

	RegAssoc.RestoreFileAssociations(toRestore); 
	return RegAssoc.CreateFileAssociations(current); 
}

void WinFileAssocPage::saveAssociations()
{
	QStringList extensions; 

    for (int k = 0; k < m_ui->listWidget->count(); k++)
	{
        QListWidgetItem* pItem = m_ui->listWidget->item(k);
		if (pItem && pItem->checkState() == Qt::Checked)
			extensions.append(pItem->text()); 
	}

    if(extensions == m_regExtensions)
        return;

	int processed = ProcessAssociations(extensions, m_regExtensions); 

	if (processed != extensions.count())
	{
		QMessageBox::warning(this, tr("Warning"), 
            tr("Not all files could be associated. Please check your "
               "security permissions and retry."), QMessageBox::Ok);
	}
}

void WinFileAssocPage::createHelp()
{
    m_ui->selectAll->setToolTip(tr("Check all file types in the list"));
    m_ui->selectNone->setToolTip(tr("Uncheck all file types in the list"));
    m_ui->listWidget->setToolTip(tr("Check the media file extensions you would like Qmmp to handle. "
                                    "When you click Apply, the checked files will be associated with "
                                    "Qmmp. If you uncheck a media type, the file association will "
                                    "be restored.") + "<br>" +
                                 tr("<b>Note:</b> Restoration doesn't work on Windows Vista/7."));
}