00001 /****************************************************************************** 00002 * Copyright (C) 2015 by Ralf Kaestner * 00003 * ralf.kaestner@gmail.com * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the Lesser GNU General Public License as published by* 00007 * the Free Software Foundation; either version 3 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * Lesser GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the Lesser GNU General Public License * 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00017 ******************************************************************************/ 00018 00019 #include <QDir> 00020 #include <QMutexLocker> 00021 00022 #include <ros/package.h> 00023 00024 #include "rqt_multiplot/PackageRegistry.h" 00025 00026 namespace rqt_multiplot { 00027 00028 /*****************************************************************************/ 00029 /* Static Initializations */ 00030 /*****************************************************************************/ 00031 00032 PackageRegistry::Impl PackageRegistry::impl_; 00033 00034 /*****************************************************************************/ 00035 /* Constructors and Destructor */ 00036 /*****************************************************************************/ 00037 00038 PackageRegistry::PackageRegistry(QObject* parent) : 00039 QObject(parent) { 00040 connect(&impl_, SIGNAL(started()), this, SLOT(threadStarted())); 00041 connect(&impl_, SIGNAL(finished()), this, SLOT(threadFinished())); 00042 } 00043 00044 PackageRegistry::~PackageRegistry() { 00045 } 00046 00047 PackageRegistry::Impl::Impl(QObject* parent) : 00048 QThread(parent) { 00049 } 00050 00051 PackageRegistry::Impl::~Impl() { 00052 terminate(); 00053 wait(); 00054 } 00055 00056 /*****************************************************************************/ 00057 /* Accessors */ 00058 /*****************************************************************************/ 00059 00060 QMap<QString, QString> PackageRegistry::getPackages() const { 00061 QMutexLocker lock(&impl_.mutex_); 00062 00063 return impl_.packages_; 00064 } 00065 00066 bool PackageRegistry::isUpdating() const { 00067 return impl_.isRunning(); 00068 } 00069 00070 bool PackageRegistry::isEmpty() const { 00071 QMutexLocker lock(&impl_.mutex_); 00072 00073 return impl_.packages_.isEmpty(); 00074 } 00075 00076 /*****************************************************************************/ 00077 /* Methods */ 00078 /*****************************************************************************/ 00079 00080 void PackageRegistry::update() { 00081 impl_.start(); 00082 } 00083 00084 void PackageRegistry::wait() { 00085 impl_.wait(); 00086 } 00087 00088 void PackageRegistry::Impl::run() { 00089 std::vector<std::string> packages; 00090 00091 mutex_.lock(); 00092 packages_.clear(); 00093 mutex_.unlock(); 00094 00095 if (ros::package::getAll(packages)) { 00096 for (size_t i = 0; i < packages.size(); ++i) { 00097 QString package = QString::fromStdString(packages[i]); 00098 QDir directory(QString::fromStdString(ros::package:: 00099 getPath(packages[i]))); 00100 00101 if (directory.exists()) { 00102 mutex_.lock(); 00103 packages_[package] = directory.absolutePath(); 00104 mutex_.unlock(); 00105 } 00106 } 00107 } 00108 } 00109 00110 /*****************************************************************************/ 00111 /* Slots */ 00112 /*****************************************************************************/ 00113 00114 void PackageRegistry::threadStarted() { 00115 emit updateStarted(); 00116 } 00117 00118 void PackageRegistry::threadFinished() { 00119 emit updateFinished(); 00120 } 00121 00122 }