MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
Qtの基礎 - 管理者権限のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
Qtの基礎 - 管理者権限
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == <br><br> == PolKit-Qt-1 == ==== PolKit-Qt-1とは ==== PolKit-Qt-1のライセンスは、<u>LGPL-2.1</u>に基づく。<br> <br> PolKit-Qt-1は、Qtソフトウェアの開発者がPolKit APIを簡単に利用できるようにすることを目的としている。<br> <code>QAction</code>クラスと<code>QAbstractButton</code>ボタンの便利なラッパーもあり、これらのコンポーネントをPolKitと簡単に統合することもできる。<br> <br> PolKit-Qt-1は、以前のPolKit-Qtを直接置き換えるものではなく、PolKit-Qtのバックエンドであった旧PolicyKit(バージョン 0.9以下)と後方互換性はない。<br> 現在、旧PolicyKitはメンテナンスされていないため、PolKit-Qtは、PolKit-Qt-1またはKAuth(KDEフレームワーク)に移植することが推奨される。<br> <br> PolKit-Qt-1は、以下に示す3つのライブラリに分かれている。<br> * polkit-qt-core-1 *: GUIを使用せず、アクションや認証を制御することができる。 *: また、PolKitの権威に関する有用な情報を取得して、制御することができる。 *: 主に、<code>Authority</code>クラスが含まれている。 *: <br> * polkit-qt-gui-1 *: GUIアイテムをpolkitのアクションと簡単に関連付けることができる。 *: いくつかのラッパークラスを通して、<code>QAction</code>クラスや<code>QAbstractButton</code>クラスをPolKitアクションに関連付けることができ、 *: それらのプロパティをPolKitの結果に応じて変更させることができる。 *: <code>Action</codE>クラス、<code>ActionButton</code>クラス、<code>ActionButtons</code>クラスの各クラスが含まれている。 *: <br> * polkit-qt-agent-1 *: 非常にシンプルな方法により、独自のPolKit認証エージェントを記述することができる。 <br> PolKit-Qt-1の内部仕様を知りたい場合は、[https://api.kde.org/polkit-qt-1/html/index.html PolKit-Qt-1の公式ドキュメント]を参照すること。<br> <br> <u>※注意</u><br> <u>Qtライブラリだけでなく、KDEフレームワークのようなQt拡張に依存するソフトウェアを開発する場合、KDEフレームワークのKAuthを使用することを推奨する。</u><br> <br> ==== PolKit-Qt-1のインストール ==== * パッケージ管理システムからインストールする場合 *: <code>sudo zypper install libpolkit-qt5-1-devel</code> *: <br> * ソースコードからインストールする場合 *: PolKit-Qt-1のビルドに必要なライブラリをインストールする。 *: <code>sudo zypper install pkg-config polkit-devel \</code> *: <code>libQt5Core-devel libQt5Core-private-headers-devel libQt5Widgets-devel libQt5Widgets-private-headers-devel \</code> *: <code>libQt5DBus-devel libQt5DBus-private-headers-devel libQt5Xml-devel</code> *: <br> *: [https://github.com/KDE/polkit-qt-1 PolKit-Qt-1のGithub]または[https://invent.kde.org/libraries/polkit-qt-1/-/tags 公式のGitLab]にアクセスして、ソースコードをダウンロードする。 *: ダウンロードしたファイルを解凍する。 *: <code>tar xf polkit-qt-1-<バージョン>.tar.gz</code> *: <code>cd polkit-qt-1-<バージョン>.tar.gz</code> *: または、<code>git clone</code>コマンドを実行して、ソースコードをダウンロードする。 *: <code>git clone https://invent.kde.org/libraries/polkit-qt-1.git</code> *: <code>cd polkit-qt-1</code> *: <br> *: PolKit-Qt-1をインストールする。 *: <code>cmake -DCMAKE_INSTALL_PREFIX=<PolKit-Qt-1のインストールディレクトリ> -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=TRUE \</code> *: <code>-DSYSCONF_INSTALL_DIR=<PolKit-Qt-1のインストールディレクトリ> ..</code> *: <br> *: <code>make -j $(nproc)</code> *: <code>make install</code> <br> ~/.profileファイル等に、環境変数を追記する。<br> vi ~/.profile <br> # ~/.profile export LD_LIBRARY_PATH="/<PolKit-Qt-1のインストールディレクトリ>/lib64:$LD_LIBRARY_PATH" export PKG_CONFIG_PATH="/<PolKit-Qt-1のインストールディレクトリ>/lib64/pkgconfig:${PKG_CONFIG_PATH}" <br> <u>※注意</u><br> <u>Qt Creator(Qt Creatorに付属しているQtライブラリ)とPolKit-Qt-1を使用してビルドした場合、生成された実行ファイルおよびライブラリはQt Creatorに付属しているQtライブラリを指すことになる。</u><br> <u>つまり、Qt Creatorに付属しているQtライブラリを環境変数<code>PKG_CONFIG_PATH</code>等に追加しなければ生成されたファイルが動作しないということである。</u><br> <br> <u>この動作を変更する場合(生成された実行ファイルおよびライブラリを/usrディレクトリにあるQtライブラリを指すようにする場合)は、</u><br> <u>パッケージ管理システムから、少なくとも、Qt5Core、Qt5GUI、Qt5DBusをインストールする必要がある。</u><br> sudo zypper install libQt5Core-devel libQt5Gui-devel libQt5DBus-devel \ libqt5-qtbase-common-devel vulkan-devel vulkan-headers # 依存関係は自動的にインストールされる <br> <u>次に、<code>cmake</code>コマンド、または、パッケージ管理システムからインストールした<code>qmake-qt5</code>を使用して、Qtプロジェクトをビルドする必要がある。</u><br> # qmakeコマンドを使用する場合 mkdir build && cd build qmake-qt5 <Qtプロジェクトファイル名>.pro または qmake <Qtプロジェクトファイル名>.pro make -j $(nproc) make install # cmakeコマンドを使用する場合 mkdir build && cd build cmake .. make -j $(nproc) make install <br><br> == PolKit-Qt-1を使用した開発例 == ==== 開発例に挙げるソフトウェア ==== 実行ファイルの画面にあるボタンを押下する時、任意のディレクトリにroot権限でテキストファイルを作成または書き込みするソフトウェアを例に挙げる。<br> <br> 開発するプロジェクトを以下に示す。<br> * Sample *: 主となるGUIソフトウェア * SampleHelper *: 管理者権限でファイル操作を行うヘルパー実行ファイル * PolKitポリシーファイル *: ファイルの場所 : /usr/share/polkit-1/actions *: .policy拡張子 *: ファイル名を、org.qt.policykit.examples.policyファイルとする。 * D-Busインターフェースファイル *: ファイルの場所 : /usr/share/dbus-1/interfaces *: .xml拡張子 *: ファイル名を、org.qt.policykit.examples.xmlファイルとする。 * D-Busポリシーファイル *: ファイルの場所 : *:: 優先順位 1 : /etc/dbus-1/system-local.conf (新規作成) *:: 優先順位 2 : /etc/dbus-1/system.d *:: 優先順位 3 : /usr/share/dbus-1/system.d *: .conf拡張子 *: ファイル名を、org.qt.policykit.examples.confファイルとする。 * D-Busサービスファイル(D-Busアクティベーションファイル) *: ファイルの場所 : /usr/share/dbus-1/system-services *: .service拡張子 *: ファイル名を、org.qt.policykit.examples.serviceファイルとする。 <br> ==== ヘルパー実行ファイルの開発 ==== # SampleHelper.proファイル QT -= gui QT += core dbus CONFIG += c++17 CONFIG -= app_bundle # PolKit-Qt-1 Install directory isEmpty(polqt_dir) { polqt_dir = /usr } # Add PolKit-Qt-1 Library & Header directory !isEmpty(polqt_dir) { LIBS += \ -L$${polqt_dir}/lib64 -lpolkit-qt5-core-1 \ -L$${polqt_dir}/lib64 -lpolkit-qt5-agent-1 \ -L$${polqt_dir}/lib64 -lpolkit-qt5-gui-1 INCLUDEPATH += \ $${polqt_dir}/include } else { LIBS += \ -lpolkit-qt5-core-1 \ -lpolkit-qt5-agent-1 \ -lpolkit-qt5-gui-1 } SOURCES += \ SampleHelper.cpp \ SamplesAdaptor.cpp \ main.cpp HEADERS += \ SampleHelper.h \ SamplesAdaptor.h # Config Install directory isEmpty(prefix) { prefix = $${PWD}/$${TARGET}/bin } target.path = $${prefix} INSTALLS += target <br> <syntaxhighlight lang="c++"> // main.cppファイル #include <QCoreApplication> #include "SampleHelper.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); SampleHelper helper(argc, argv); return a.exec(); } </syntaxhighlight> <br> <syntaxhighlight lang="c++"> // SampleHelper.hファイル #ifndef SAMPLE_HELPER_H #define SAMPLE_HELPER_H #include <QDBusContext> #include <QDBusMessage> #include <QCoreApplication> class SampleHelper : public QCoreApplication, protected QDBusContext { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.qt.policykit.examples") public: SampleHelper(int &argc, char **argv); ~SampleHelper() override; public Q_SLOTS: bool write(const QString &action); bool writeValue(const QString &action); }; #endif </syntaxhighlight> <br> <syntaxhighlight lang="c++"> // SampleHelper.cppファイル #include "SampleHelper.h" #include "SamplesAdaptor.h" #include <polkit-qt5-1/polkitqt1-authority.h> #include <QDBusConnection> #include <QTimer> #include <QDebug> #include <QFile> #define MINUTE 60000 using namespace PolkitQt1; SampleHelper::SampleHelper(int &argc, char **argv) : QCoreApplication(argc, argv) { qDebug() << "Creating Helper"; (void) new ExamplesAdaptor(this); // Register the DBus service if (!QDBusConnection::systemBus().registerService("org.qt.policykit.examples")) { qDebug() << QDBusConnection::systemBus().lastError().message();; QTimer::singleShot(0, this, SLOT(quit())); return; } if (!QDBusConnection::systemBus().registerObject("/", this)) { qDebug() << "unable to register service interface to dbus"; QTimer::singleShot(0, this, SLOT(quit())); return; } // Normally you will set a timeout so your application can free some resources of the poor client machine QTimer::singleShot(MINUTE, this, SLOT(quit())); } SampleHelper::~SampleHelper() { qDebug() << "Destroying Helper"; } bool SampleHelper::write(const QString &action) { // message().service() is the service name of the caller. // We can check if the caller is authorized to the following action. Authority::Result result; PolkitQt1::SystemBusNameSubject subject(message().service()); result = Authority::instance()->checkAuthorizationSync("org.qt.policykit.examples.write", subject, Authority::AllowUserInteraction); if (result == Authority::Yes) { // Caller is authorized so we can perform the action return writeValue(action); } else { // Caller is not authorized so the action can't be performed return false; } } bool SampleHelper::writeValue(const QString &action) { // This action must be authorized first. It will set the implicit authorization for the Shout action by editing the .policy file. QFileInfo FileInfo("/opt/sample.txt"); QFile File("/opt/sample.txt"); if(!File.open(QIODevice::WriteOnly)) { QString strErrMsg = "File(" + FileInfo.fileName() + ") Open Error: " + File.errorString(); qDebug() << strErrMsg; return -1; } QTextStream OutStream(&File); OutStream << "foo bar"; File.close(); return true; } </syntaxhighlight> <br> ここで、SamplesAdaptor.cppファイル、および、SamplesAdaptor.hファイルは、Qt Creator付属の<code>qdbusxml2cpp</code>コマンドを実行することにより自動生成される。<br> 自動生成された上記の2つのファイルをインクルードすることにより、ヘルパー実行ファイルが完成する。<br> # mocファイルはインクルードしない場合 qdbusxml2cpp -a <生成するcppファイル名とヘッダファイル名> -c <自動生成するヘルパークラス名 (親クラス)> -i <対象となるクラスを記述しているヘッダファイル> -l <対象となるクラス名> <D-Busインターフェースファイルのパス> 例. qdbusxml2cpp -a SamplesAdaptor -c SamplesAdaptor -i SampleHelper.h -l SampleHelper org.qt.policykit.examples.xml # mocファイルもインクルードする場合 qdbusxml2cpp -m -a <生成するcppファイル名とヘッダファイル名> -c <自動生成するヘルパークラス名 (親クラス)> -i <対象となるクラスを記述しているヘッダファイル> -l <対象となるクラス名> <D-Busインターフェースファイルのパス> 例. qdbusxml2cpp -m -a SamplesAdaptor -c SamplesAdaptor -i SampleHelper.h -l SampleHelper org.qt.policykit.examples.xml <br> 自動生成されたヘルパーファイルを、Qtプロジェクト等に追加する。<br> <br> 最後に、<code>cmake</code>コマンド、または、パッケージ管理システムからインストールした<code>qmake-qt5</code>を使用して、Qtプロジェクトをビルドする。<br> # qmakeコマンドを使用する場合 mkdir build && cd build qmake-qt5 <ヘルパープロジェクトファイル名>.pro または qmake <ヘルパープロジェクトファイル名>.pro make -j $(nproc) make install # cmakeコマンドを使用する場合 mkdir build && cd build cmake .. make -j $(nproc) make install <br><br> __FORCETOC__ [[カテゴリ:Qt]]
Qtの基礎 - 管理者権限
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse