~aleteoryx/muditaos

ref: 266c2085296f7369f7a4c4571e544e31258b4c7b muditaos/module-services/service-fileindexer/ServiceFileIndexer.cpp -rw-r--r-- 5.8 KiB
266c2085 — Wojtek Rzepecki [EGD-6790] Fix cellular sleep mode 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log/log.hpp>
#include <purefs/filesystem_paths.hpp>
#include <purefs/fs/inotify.hpp>
#include "ServiceFileIndexer.hpp"
#include <fileref.h>
#include <tag.h>
#include "notesIndexer.hpp"
#include "Constants.hpp"
#include <purefs/fs/inotify_message.hpp>

namespace
{
    inline auto getMusicPath()
    {
        return purefs::createPath(purefs::dir::getUserDiskPath(), "music").string();
    }
} // namespace

namespace service
{

    ServiceFileIndexer::ServiceFileIndexer(const std::string_view name) : sys::Service(std::string(name))
    {
        LOG_DEBUG("[%s] Initializing", std::string(name).c_str());
    }

    // When receive notification handler
    sys::MessagePointer ServiceFileIndexer::DataReceivedHandler(sys::DataMessage *msg, sys::ResponseMessage *resp)
    {
        auto inotify = dynamic_cast<purefs::fs::message::inotify *>(msg);
        if (inotify) {
            LOG_ERROR("Inotify event %s %08x", inotify->name.c_str(), int(inotify->flags));
        }
        else {
            LOG_ERROR("Not a inotify message");
        }

#if 0
        auto fcm = dynamic_cast<msg::FileChangeMessage *>(msg);
        if (fcm) {
            switch (fcm->event()) {
            case msg::FileChangeMessage::evt_t::modified: {
                switch (detail::StartupIndexer::getFileType(fcm->newPath())) {
                case detail::mimeType::audio:
                    onAudioContentChanged(fcm->newPath());
                    break;
                case detail::mimeType::text:
                    onTextContentChanged(fcm->newPath());
                    break;
                default:
                    LOG_INFO("Skip indexing file %s", fcm->newPath().c_str());
                    break;
                }
            } break;
            case msg::FileChangeMessage::evt_t::renamed:
                onRenameFile(fcm->oldPath(), fcm->newPath());
                break;
            case msg::FileChangeMessage::evt_t::deleted:
                onDeleteFile(fcm->newPath());
                break;
            case msg::FileChangeMessage::evt_t::initialized:
                break;
            }
            return std::make_shared<sys::ResponseMessage>();
        }
#endif
        return std::make_shared<sys::ResponseMessage>(sys::ReturnCodes::Unresolved);
    }

    // Initialize data notification handler
    sys::ReturnCodes ServiceFileIndexer::InitHandler()
    {
        /*
        mStartupIndexer.start(shared_from_this(), service::name::file_indexer);
        */

        mfsNotifier = purefs::fs::inotify_create(shared_from_this());
        if (!mfsNotifier) {
            LOG_ERROR("Unable to create inotify object");
            return sys::ReturnCodes::Failure;
        }
        const int err = mfsNotifier->add_watch(getMusicPath(), purefs::fs::inotify_flags::close_write);
        if (err) {
            LOG_ERROR("Unable to create inotify watch errno: %i", err);
            return sys::ReturnCodes::Failure;
        }
        return sys::ReturnCodes::Success;
    }

    sys::ReturnCodes ServiceFileIndexer::DeinitHandler()
    {
        const int err = mfsNotifier->rm_watch(getMusicPath());
        if (err) {
            LOG_ERROR("Unable to remove watch errno: %i", err);
            return sys::ReturnCodes::Failure;
        }
        return sys::ReturnCodes::Success;
    }

    sys::ReturnCodes ServiceFileIndexer::SwitchPowerModeHandler(const sys::ServicePowerMode mode)
    {
        LOG_DEBUG("Switch to power Mode %s", c_str(mode));
        return sys::ReturnCodes::Success;
    }

    // When file is changed update db only
    auto ServiceFileIndexer::onDeleteFile(std::string_view path) -> void
    {
        LOG_DEBUG("File deleted %s", std::string(path).c_str());
    }
    // When file is renamed
    auto ServiceFileIndexer::onRenameFile(std::string_view oldPath, std::string_view newPath) -> void
    {
        LOG_DEBUG("File renamed old: %s, new: %s", std::string(oldPath).c_str(), std::string(newPath).c_str());
    }
    // On audio file content change
    auto ServiceFileIndexer::onAudioContentChanged(std::string_view path) -> void
    {
        LOG_DEBUG("Audio content index %s", std::string(path).c_str());
        TagLib::FileRef fref(std::string(path).c_str());
        if (!fref.isNull() && fref.tag()) {
            const auto tag = fref.tag();
            LOG_DEBUG(">>>>> title %s", tag->title().toCString());
            LOG_DEBUG(">>>>> artist %s", tag->artist().toCString());
            LOG_DEBUG(">>>> album %s", tag->album().toCString());
            LOG_DEBUG(">>>>> year %i", tag->year());
            LOG_DEBUG(">>>>> comment %s", tag->comment().toCString());
            LOG_DEBUG(">>>> track %u", tag->track());
            LOG_DEBUG(">>>> genre %s", tag->genre().toCString());
        }
        if (!fref.isNull() && fref.audioProperties()) {
            const auto prop = fref.audioProperties();
            int seconds     = prop->length() % 60;
            int minutes     = (prop->length() - seconds) / 60;
            LOG_DEBUG(">>>>> bitrate %i", prop->bitrate());
            LOG_DEBUG(">>>>> samplerate %i", prop->sampleRate());
            LOG_DEBUG(">>>>> channels %i", prop->channels());
            LOG_DEBUG(">>>>> length %02i:%02i", minutes, seconds);
        }
    }
    // On text file content change
    auto ServiceFileIndexer::onTextContentChanged(std::string_view path) -> void
    {
        LOG_DEBUG("Text content index %s", std::string(path).c_str());
        detail::notesIndexer noteInfo(path);
        LOG_DEBUG("Words %zu Lines %zu Chars %zu Size %zu",
                  noteInfo.getWords(),
                  noteInfo.getLines(),
                  noteInfo.getChars(),
                  noteInfo.getFileSize());
    }
} // namespace service