add basic export-partitiontable-feature

svn path=/trunk/extragear/sysadmin/partitionmanager/; revision=1097939
This commit is contained in:
Volker Lanz 2010-03-02 13:35:57 +00:00
parent 142d49800a
commit 3392940beb
8 changed files with 121 additions and 7 deletions

2
TODO
View File

@ -17,5 +17,7 @@ Random plans and ideas for 1.1 and beyond:
* The KPart works but has problems: The context menus don't show up, the status
bar is missing, maybe more.
* save msdos mbr?
Bugs:

View File

@ -335,3 +335,29 @@ void Partition::move(qint64 newStartSector)
setFirstSector(newStartSector);
setLastSector(newStartSector + savedLength - 1);
}
QTextStream& operator<<(QTextStream& stream, const Partition& p)
{
QStringList flagList;
foreach(const PartitionTable::Flag& f, PartitionTable::flagList())
{
if (p.activeFlags() & f)
flagList.append(PartitionTable::flagName(f));
}
const QString sep = ";";
// number - start - end - type - roles - label - flags
stream << p.number() << sep
<< p.firstSector() << sep
<< p.lastSector() << sep
<< p.fileSystem().name() << sep
<< p.roles().toString() << sep
<< "\"" << p.fileSystem().label() << "\"" << sep
<< "\"" << flagList.join(",") << "\""
<< "\n";
return stream;
}

View File

@ -57,6 +57,7 @@ class FileSystem;
class Report;
class QString;
class QTextStream;
/** @brief A partition or some unallocated space on a Device.
@ -94,6 +95,8 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT Partition : public PartitionNode
friend class SetPartFlagsJob;
friend class RestoreFileSystemJob;
friend QTextStream& operator<<(QTextStream& stream, const Partition& p);
public:
/** A Partition state -- where did it come from? */
enum State
@ -200,5 +203,7 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT Partition : public PartitionNode
State m_State;
};
QTextStream& operator<<(QTextStream& stream, const Partition& p);
#endif

View File

@ -32,6 +32,9 @@
#include <kdebug.h>
#include <klocale.h>
#include <QFile>
#include <QTextStream>
#include <config.h>
/** Creates a new PartitionTable object with type MSDOS
@ -682,3 +685,34 @@ void PartitionTable::setType(const Device& d, TableType t)
m_Type = t;
}
static bool isPartitionLessThan(const Partition* p1, const Partition* p2)
{
return p1->number() < p2->number();
}
QTextStream& operator<<(QTextStream& stream, const PartitionTable& ptable)
{
stream << "type: \"" << ptable.typeName() << "\"\n\n"
<< "# number start end type roles label flags\n";
QList<const Partition*> partitions;
foreach(const Partition* p, ptable.children())
if (!p->roles().has(PartitionRole::Unallocated))
{
partitions.append(p);
if (p->roles().has(PartitionRole::Extended))
foreach(const Partition* child, p->children())
if (!child->roles().has(PartitionRole::Unallocated))
partitions.append(child);
}
qSort(partitions.begin(), partitions.end(), isPartitionLessThan);
foreach(const Partition* p, partitions)
stream << *p;
return stream;
}

View File

@ -33,6 +33,8 @@ class Device;
class Partition;
class CoreBackend;
class QTextStream;
/** @brief The partition table (a.k.a Disk Label)
PartitionTable represents a partition table (or disk label).
@ -46,6 +48,7 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT PartitionTable : public PartitionNode
Q_DISABLE_COPY(PartitionTable)
friend class CoreBackend;
friend QTextStream& operator<<(QTextStream& stream, const PartitionTable& ptable);
public:
enum TableType
@ -144,6 +147,7 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT PartitionTable : public PartitionNode
static bool tableTypeSupportsExtended(TableType l);
static bool tableTypeIsReadOnly(TableType l);
protected:
void setMaxPrimaries(qint32 n) { m_MaxPrimaries = n; }
void setFirstUsableSector(qint64 s) { m_FirstUsable = s; }
@ -159,6 +163,7 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT PartitionTable : public PartitionNode
Q_DECLARE_OPERATORS_FOR_FLAGS(PartitionTable::Flags)
QTextStream& operator<<(QTextStream& stream, const PartitionTable& ptable);
#endif

View File

@ -54,10 +54,14 @@
#include <kapplication.h>
#include <kmenu.h>
#include <kxmlguifactory.h>
#include <kfiledialog.h>
#include <QCloseEvent>
#include <QReadLocker>
#include <QPointer>
#include <QFile>
#include <QDateTime>
#include <QTextStream>
#include <config.h>
@ -217,6 +221,13 @@ void MainWindow::setupActions()
createNewPartitionTable->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_N);
createNewPartitionTable->setIcon(BarIcon("edit-clear"));
KAction* exportPartitionTable = actionCollection()->addAction("exportPartitionTable", this, SLOT(onExportPartitionTable()));
exportPartitionTable->setEnabled(false);
exportPartitionTable->setText(i18nc("@action:inmenu", "Export Partition Table"));
exportPartitionTable->setToolTip(i18nc("@info:tooltip", "Export a partition table"));
exportPartitionTable->setStatusTip(i18nc("@info:status", "Exports the device's partition table in sfdisk-compatible format to a text file."));
exportPartitionTable->setIcon(BarIcon("document-export"));
KAction* propertiesDevice = actionCollection()->addAction("propertiesDevice", this, SLOT(onPropertiesDevice()));
propertiesDevice->setText(i18nc("@action:inmenu", "Properties"));
propertiesDevice->setToolTip(i18nc("@info:tooltip", "Show device properties dialog"));
@ -359,6 +370,7 @@ void MainWindow::saveConfig() const
void MainWindow::enableActions()
{
actionCollection()->action("createNewPartitionTable")->setEnabled(CreatePartitionTableOperation::canCreate(pmWidget().selectedDevice()));
actionCollection()->action("exportPartitionTable")->setEnabled(pmWidget().selectedDevice() && pmWidget().selectedDevice()->partitionTable() && numPendingOperations() == 0);
actionCollection()->action("undoOperation")->setEnabled(numPendingOperations() > 0);
actionCollection()->action("clearAllOperations")->setEnabled(numPendingOperations() > 0);
@ -654,6 +666,34 @@ void MainWindow::onCreateNewPartitionTable()
delete dlg;
}
void MainWindow::onExportPartitionTable()
{
Q_ASSERT(pmWidget().selectedDevice());
Q_ASSERT(pmWidget().selectedDevice()->partitionTable());
QString fileName = KFileDialog::getSaveFileName(KUrl("kfiledialog://exportPartitionTable"));
if (fileName.isEmpty())
return;
if (QFile::exists(fileName) && KMessageBox::warningContinueCancel(this, i18nc("@info", "Do you want to overwrite the existing file <filename>%1</filename>?", fileName), i18nc("@title:window", "Overwrite Existing File?"), KGuiItem(i18nc("@action:button", "&Overwrite File")), KStandardGuiItem::cancel()) != KMessageBox::Continue)
return;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
{
KMessageBox::error(this, i18nc("@info", "Could not create output file <filename>%1</filename>.", fileName), i18nc("@window:title", "Error Exporting Partition Table"));
return;
}
QTextStream stream(&file);
stream << "# partition table of " << pmWidget().selectedDevice()->deviceNode() << "\n";
stream << "# on " << QDateTime::currentDateTime().toString() << "\n";
stream << *pmWidget().selectedDevice()->partitionTable();
}
void MainWindow::onFileSystemSupport()
{
FileSystemSupportDialog dlg(this);

View File

@ -148,6 +148,7 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT MainWindow : public KXmlGuiWindow, publi
void onRefreshDevices();
void onCreateNewPartitionTable();
void onExportPartitionTable();
void onApplyAllOperations();
void onUndoOperation();

View File

@ -1,6 +1,6 @@
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<gui name="KDE Partition Manager" version="9">
<gui name="KDE Partition Manager" version="10">
<ToolBar name="editToolBar">
<text context="@title:menu turn on and off edit toolbar">Edit Toolbar</text>
<Action name="applyAllOperations"/>
@ -38,13 +38,14 @@
<Action name="toggleDockOperations"/>
<Action name="toggleDockInformation"/>
<Action name="toggleDockLog"/>
<separator name="separator"/>
<separator/>
<Action name="fileSystemSupport"/>
<Action name="refreshDevices"/>
</Menu>
<Menu name="device">
<text context="@title:menu">Device</text>
<Action name="createNewPartitionTable"/>
<Action name="exportPartitionTable"/>
<Action name="propertiesDevice"/>
</Menu>
<Menu name="partition">
@ -53,18 +54,18 @@
<Action name="resizePartition"/>
<Action name="deletePartition"/>
<Action name="shredPartition"/>
<separator name="separator"/>
<separator/>
<Action name="copyPartition"/>
<Action name="pastePartition"/>
<separator name="separator"/>
<separator/>
<Action name="editMountPoint"/>
<Action name="mountPartition"/>
<separator name="separator"/>
<separator/>
<Action name="checkPartition"/>
<separator name="separator"/>
<separator/>
<Action name="backupPartition"/>
<Action name="restorePartition"/>
<separator name="separator"/>
<separator/>
<Action name="propertiesPartition"/>
</Menu>
<Menu name="settings"/>