Commit 46de3c16 authored by Lisa's avatar Lisa

First implementation of the cache cleaner tool.

parent 6ad70ce4
......@@ -28,6 +28,8 @@ apm.cpp
cmdline.cpp
main.cpp)
add_subdirectory(tools)
#qt4_automoc(${guzuta_SRCS})
kde4_add_executable(akabei ${apm_SRCS})
target_link_libraries(akabei ${KDE4_KDEUI_LIBRARY} ${KDE4_KIO_LIBRARY} ${AKABEICLIENT_LIBRARIES})
......
add_subdirectory(akabei-clean-cache)
\ No newline at end of file
project(akabei)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
find_package(KDE4 REQUIRED)
find_package(AkabeiCore REQUIRED)
include (KDE4Defaults)
set(akabei_clean_cache_SRCS main.cpp)
include_directories(${QT_INCLUDES} ${KDE4_INCLUDE_DIR} ${AKABEICORE_INCLUDE_DIR})
kde4_add_executable(akabei-clean-cache ${akabei_clean_cache_SRCS})
target_link_libraries(akabei-clean-cache ${KDE4_KDEUI_LIBRARY} ${AKABEICORE_LIBRARIES})
install(TARGETS akabei-clean-cache ${INSTALL_TARGETS_DEFAULT_ARGS})
/*
* This file is part of the Chakra project
* Source of the tool akabei-clean-cache
Copyright (C) 2010 Lisa Vitolo <shainer@chakra-project.org>
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.
*/
#include <kcmdlineargs.h>
#include <kaboutdata.h>
#include <QSettings>
#include <QStringList>
#include <QCoreApplication>
#include <akabeicache.h>
#include <akabeibackend.h>
using namespace std;
/*
* Attempt to read a cache directory from the default configuration file
*/
QString readCacheDirFromConfig()
{
QSettings settings;
QTextStream err(stderr);
if (!QFile::exists(settings.fileName())) {
err << "[!!] Configuration file \"" << settings.fileName() << "\" doesn't exist." << endl;
err << "Please create one before continuing." << endl;
exit(-1);
}
if (settings.contains("CacheDir")) {
return settings.value("CacheDir").toString();
}
return "/var/lib/akabei"; /* TODO: change to whatever the default path is */
}
int main(int argc, char **argv)
{
/*
* This lines are needed to tell QSettings where the config file is located
*/
QCoreApplication::setOrganizationName("The Chakra Project");
QCoreApplication::setOrganizationDomain("chakra-project.org");
QCoreApplication::setApplicationName("Akabei");
KAboutData aboutData("Akabei", 0, ki18n("akabei-clean-cache"),
QByteArray("0.0.1"),
ki18n("Akabei's cache cleaner tool"),
KAboutData::License_GPL,
ki18n("(C) 2011 Lisa Vitolo"),
ki18n("<a href=\"mailto:shainer@chakra-project.org\">shainer@chakra-project.org</a>"));
aboutData.addAuthor(ki18n("Lisa Vitolo"), ki18n("Maintainer"), "shainer@chakra-project.org");
KCmdLineArgs::init(argc, argv, &aboutData);
KCmdLineOptions cacheoptions;
cacheoptions.add("c");
cacheoptions.add("cachedir <path>", ki18n("Set cache directory."));
cacheoptions.add("k");
cacheoptions.add("keep-package <package>", ki18n("Never delete <package> from cache."));
cacheoptions.add("o");
cacheoptions.add("clean-old", ki18n("Clean the oldest packages in cache."));
cacheoptions.add("u");
cacheoptions.add("clean-unused", ki18n("Clean the least used packages in cache."));
cacheoptions.add("a");
cacheoptions.add("clean-all", ki18n("Clean all cache (default)"));
KCmdLineArgs::addCmdLineOptions(cacheoptions);
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
/*
* Command line wins over configuration file
*/
QString cachedir;
if (args->isSet("cachedir")) {
cachedir = args->getOptionList("cachedir").last();
} else {
cachedir = readCacheDirFromConfig();
}
QTextStream err(stderr);
QDir cd(cachedir);
if (!cd.exists()) {
err << "[!!] Directory \"" << cachedir << "\" doesn't exist." << endl;
err << "Please select another one using the --cachedir option." << endl;
return -1;
}
QStringList keepPackages = args->getOptionList("keep-package");
QSettings settings;
settings.beginGroup("cache");
if (!settings.value("KeepPackages").isNull()) {
keepPackages.append(settings.value("KeepPackages").toString().split(' '));
}
keepPackages.removeDuplicates();
settings.endGroup();
QHash<QString, bool> cleanOptions;
cleanOptions["clean-old"] = false;
cleanOptions["clean-unused"] = false;
cleanOptions["clean-all"] = false;
if (args->isSet("clean-old")) {
cleanOptions["clean-old"] = true;
}
if (args->isSet("clean-unused")) {
cleanOptions["clean-unused"] = true;
}
/* Default value if nothing is set */
if (args->isSet("clean-all") || cleanOptions.values().count(false) == cleanOptions.values().size()) {
cleanOptions["clean-all"] = true;
}
Akabei::AkabeiCache cache(cachedir, keepPackages, cleanOptions);
if (!cache.errorString().isEmpty()) {
err << "[!!] An error occurred while performing cache cleaning: " << cache.errorString() << endl;
return -1;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment