<commit_before>/* -*- Mode: C; indent-tabs-mode: s; c-basic-offset: 4; tab-width: 4 -*- */
/* vim:set et ai sw=4 ts=4 sts=4: tw=80 cino="(0,W2s,i2s,t0,l1,:0" */

#include "wallpaperbusinesslogic.h"
#include "wallpaperdescriptor.h"
#include "wallpapercurrentdescriptor.h"
#include "wallpaperitrans.h"

#include <QDir>
#include <QFile>
#include <QString>
#include <QStringList>
#include <QProcessEnvironment>
#include <QPainter>

#include <Tracker>

#include <MTheme>
#include <MGConfItem>

#define DEBUG
#include "../debug.h"

static const QString PortraitKey = 
    "/desktop/meego/background/portrait/picture_filename";
static const QString LandscapeKey = 
    "/desktop/meego/background/landscape/picture_filename";

static const QString wallpaperDir = ".wallpapers";
static const QString destopFileName = "wallpaper.desktop";
static const QString backupExtension = ".BAK";

WallpaperBusinessLogic::WallpaperBusinessLogic()
{
    WallpaperCurrentDescriptor *currentDesc;
    
    m_LandscapeGConfItem = new MGConfItem (LandscapeKey);
    m_PortraitGConfItem = new MGConfItem (PortraitKey);
    m_EditedImage = 0;

    currentDesc = WallpaperCurrentDescriptor::instance ();
    currentDesc->setFromDestopFile (dirPath() + destopFileName);
}

WallpaperBusinessLogic::~WallpaperBusinessLogic()
{
    delete m_LandscapeGConfItem;
    delete m_PortraitGConfItem;
}

/*!
 * FIXME: This method is deprecated, should use the 
 * WallpaperCurrentDescriptor::instance () instead.
 */
WallpaperDescriptor *
WallpaperBusinessLogic::Wallpaper (
        bool portrait)
{
    Q_UNUSED (portrait);
    SYS_WARNING ("Deprecated method.");
    return WallpaperCurrentDescriptor::instance ();
}

/*!
 * A high level method to set the current wallpaper.
 */
void
WallpaperBusinessLogic::setBackground (
        WallpaperITrans     *landscapeITrans,
        WallpaperITrans     *portraitITrans,
        WallpaperDescriptor *desc)
{
    bool success;

    SYS_DEBUG ("******** Saving background *********");
    if (desc == 0)
        desc = m_EditedImage;

    Q_ASSERT (landscapeITrans);
    Q_ASSERT (portraitITrans);
    Q_ASSERT (desc);

    success = ensureHasDirectory ();
    if (!success)
        return;

    createBackupFiles ();

    success = writeFiles (landscapeITrans, portraitITrans, desc);
    if (!success) {
        // FIXME: Should restore backup files here.
        return;
    }
   
    deleteBackupFiles ();

    /*
     *
     */
    WallpaperCurrentDescriptor *currentDesc;
    currentDesc = WallpaperCurrentDescriptor::instance ();
    currentDesc->setFromDestopFile (dirPath() + destopFileName);
    emit wallpaperChanged ();

    SYS_DEBUG ("****** End saving backgroun ********");
}

/*!
 * This function does not support image manipulatios, it is deprecated.
 * FIXME: To remove this function.
 */
void
WallpaperBusinessLogic::setBackground (
        WallpaperDescriptor *desc)
{
    if (desc == 0)
        desc = m_EditedImage;

    SYS_WARNING ("Deprecated method.");
#if 0
    Q_ASSERT (m_PortraitGConfItem != 0);
    Q_ASSERT (m_LandscapeGConfItem != 0);

    SYS_DEBUG ("*** filename = %s", SYS_STR(desc->filename()));
    m_PortraitGConfItem->set (desc->filename());
    m_LandscapeGConfItem->set (desc->filename());
#endif
}



/*!
 * Returns a list wallpaper descriptors with the available wallpapers. The
 * current wallpaper is the first element of the available wallpapers.
 *
 * FIXME: This function needs some polishing.
 */
QList<WallpaperDescriptor *>
WallpaperBusinessLogic::availableWallpapers () const
{
    QList<WallpaperDescriptor *> list;
    const QString query = 
"SELECT ?uri ?title ?mime ?height ?width WHERE { "
" ?item nie:url ?uri." 

" ?item nie:mimeType ?mime." 
" FILTER regex(?mime, \"image\")."

" ?item nfo:height ?height."
" ?item nfo:width ?width."
" FILTER ( ?height > \"300\" )."
" FILTER ( ?width  > \"300\" )."

" OPTIONAL { ?item nie:title ?title }."
"}"
;

    WallpaperCurrentDescriptor *currentDesc;

    currentDesc = WallpaperCurrentDescriptor::instance ();
    list << WallpaperCurrentDescriptor::instance ();

    QVector<QStringList> result = ::tracker()->rawSparqlQuery(query);
    int x = 0;
    int y = 0;

    SYS_WARNING ("*** result.size() = %d", result.size());
    foreach (QStringList partlist, result) {
        WallpaperDescriptor *desc;

        SYS_DEBUG ("*** url     = %s", SYS_STR(partlist[FieldUrl]));

        desc = new WallpaperDescriptor;
        desc->setUrl (partlist[FieldUrl]);
        desc->setTitle (partlist[FieldTitle]);
        desc->setMimeType (partlist[FieldMime]);
        list << desc;
<commit_msg>Fix for CID 618: UNUSED_VALUE<commit_after>/* -*- Mode: C; indent-tabs-mode: s; c-basic-offset: 4; tab-width: 4 -*- */
/* vim:set et ai sw=4 ts=4 sts=4: tw=80 cino="(0,W2s,i2s,t0,l1,:0" */

#include "wallpaperbusinesslogic.h"
#include "wallpaperdescriptor.h"
#include "wallpapercurrentdescriptor.h"
#include "wallpaperitrans.h"

#include <QDir>
#include <QFile>
#include <QString>
#include <QStringList>
#include <QProcessEnvironment>
#include <QPainter>

#include <Tracker>

#include <MTheme>
#include <MGConfItem>

#define DEBUG
#include "../debug.h"

static const QString PortraitKey = 
    "/desktop/meego/background/portrait/picture_filename";
static const QString LandscapeKey = 
    "/desktop/meego/background/landscape/picture_filename";

static const QString wallpaperDir = ".wallpapers";
static const QString destopFileName = "wallpaper.desktop";
static const QString backupExtension = ".BAK";

WallpaperBusinessLogic::WallpaperBusinessLogic()
{
    WallpaperCurrentDescriptor *currentDesc;
    
    m_LandscapeGConfItem = new MGConfItem (LandscapeKey);
    m_PortraitGConfItem = new MGConfItem (PortraitKey);
    m_EditedImage = 0;

    currentDesc = WallpaperCurrentDescriptor::instance ();
    currentDesc->setFromDestopFile (dirPath() + destopFileName);
}

WallpaperBusinessLogic::~WallpaperBusinessLogic()
{
    delete m_LandscapeGConfItem;
    delete m_PortraitGConfItem;
}

/*!
 * FIXME: This method is deprecated, should use the 
 * WallpaperCurrentDescriptor::instance () instead.
 */
WallpaperDescriptor *
WallpaperBusinessLogic::Wallpaper (
        bool portrait)
{
    Q_UNUSED (portrait);
    SYS_WARNING ("Deprecated method.");
    return WallpaperCurrentDescriptor::instance ();
}

/*!
 * A high level method to set the current wallpaper.
 */
void
WallpaperBusinessLogic::setBackground (
        WallpaperITrans     *landscapeITrans,
        WallpaperITrans     *portraitITrans,
        WallpaperDescriptor *desc)
{
    bool success;

    SYS_DEBUG ("******** Saving background *********");
    if (desc == 0)
        desc = m_EditedImage;

    Q_ASSERT (landscapeITrans);
    Q_ASSERT (portraitITrans);
    Q_ASSERT (desc);

    success = ensureHasDirectory ();
    if (!success)
        return;

    createBackupFiles ();

    success = writeFiles (landscapeITrans, portraitITrans, desc);
    if (!success) {
        // FIXME: Should restore backup files here.
        return;
    }
   
    deleteBackupFiles ();

    /*
     *
     */
    WallpaperCurrentDescriptor *currentDesc;
    currentDesc = WallpaperCurrentDescriptor::instance ();
    currentDesc->setFromDestopFile (dirPath() + destopFileName);
    emit wallpaperChanged ();

    SYS_DEBUG ("****** End saving backgroun ********");
}

/*!
 * This function does not support image manipulatios, it is deprecated.
 * FIXME: To remove this function.
 */
void
WallpaperBusinessLogic::setBackground (
        WallpaperDescriptor *desc)
{
    if (desc == 0)
        desc = m_EditedImage;

    SYS_WARNING ("Deprecated method.");
#if 0
    Q_ASSERT (m_PortraitGConfItem != 0);
    Q_ASSERT (m_LandscapeGConfItem != 0);

    SYS_DEBUG ("*** filename = %s", SYS_STR(desc->filename()));
    m_PortraitGConfItem->set (desc->filename());
    m_LandscapeGConfItem->set (desc->filename());
#endif
}



/*!
 * Returns a list wallpaper descriptors with the available wallpapers. The
 * current wallpaper is the first element of the available wallpapers.
 *
 * FIXME: This function needs some polishing.
 */
QList<WallpaperDescriptor *>
WallpaperBusinessLogic::availableWallpapers () const
{
    QList<WallpaperDescriptor *> list;
    const QString query = 
"SELECT ?uri ?title ?mime ?height ?width WHERE { "
" ?item nie:url ?uri." 

" ?item nie:mimeType ?mime." 
" FILTER regex(?mime, \"image\")."

" ?item nfo:height ?height."
" ?item nfo:width ?width."
" FILTER ( ?height > \"300\" )."
" FILTER ( ?width  > \"300\" )."

" OPTIONAL { ?item nie:title ?title }."
"}"
;

/*  DEFECT: UNUSED_VALUE
    WallpaperCurrentDescriptor *currentDesc;

    currentDesc = WallpaperCurrentDescriptor::instance ();
 */
    list << WallpaperCurrentDescriptor::instance ();

    QVector<QStringList> result = ::tracker()->rawSparqlQuery(query);
    int x = 0;
    int y = 0;

    SYS_WARNING ("*** result.size() = %d", result.size());
    foreach (QStringList partlist, result) {
        WallpaperDescriptor *desc;

        SYS_DEBUG ("*** url     = %s", SYS_STR(partlist[FieldUrl]));

        desc = new WallpaperDescriptor;
        desc->setUrl (partlist[FieldUrl]);
        desc->setTitle (partlist[FieldTitle]);
        desc->setMimeType (partlist[FieldMime]);
        list << desc;

{'fraction_non_alphanumeric': 0.13256796274233726, 'fraction_numerical': 0.006173508068883353, 'mean_word_length': 3.8370874803562076, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6194025', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2812, 'n_words': 672}