Loading...
Searching...
No Matches
ProtocolLibrary.hpp
1#pragma once
2#include <Device/Loading/JamomaDeviceLoader.hpp>
3#include <Device/Loading/ScoreDeviceLoader.hpp>
4#include <Device/Loading/TouchOSCDeviceLoader.hpp>
5#include <Device/Protocol/ProtocolList.hpp>
6
7#include <Explorer/Commands/Add/LoadDevice.hpp>
8#include <Explorer/DocumentPlugin/DeviceDocumentPlugin.hpp>
9#include <Explorer/Explorer/Widgets/DeviceEditDialog.hpp>
10
11#include <Library/LibraryInterface.hpp>
12#include <Protocols/Mapper/MapperDevice.hpp>
13
14#include <score/command/Dispatchers/CommandDispatcher.hpp>
15#include <score/document/DocumentContext.hpp>
17#include <score/serialization/MapSerialization.hpp>
18#include <score/widgets/MessageBox.hpp>
19
20#include <ossia-qt/js_utilities.hpp>
21
22#include <QFileInfo>
23#include <QMainWindow>
24#if __has_include(<QQmlEngine>)
25#include <QQmlComponent>
26#include <QQmlEngine>
27#include <QQmlProperty>
28#endif
29#if defined(OSSIA_PROTOCOL_OSC)
30#include <Protocols/OSC/OSCProtocolFactory.hpp>
31#endif
32#if defined(OSSIA_PROTOCOL_OSCQUERY)
33#include <Protocols/Minuit/MinuitProtocolFactory.hpp>
34#endif
35#if defined(OSSIA_PROTOCOL_MINUIT)
36#include <Protocols/OSCQuery/OSCQueryProtocolFactory.hpp>
37#endif
38#if defined(OSSIA_PROTOCOL_HTTP)
39#include <Protocols/HTTP/HTTPProtocolFactory.hpp>
40#include <Protocols/HTTP/HTTPSpecificSettings.hpp>
41
42#include <ossia-qt/http/http_protocol.hpp>
43#endif
44#if defined(OSSIA_PROTOCOL_WEBSOCKETS)
45#include <Protocols/WS/WSProtocolFactory.hpp>
46#include <Protocols/WS/WSSpecificSettings.hpp>
47
48#include <ossia-qt/websocket-generic-client/ws_generic_client_protocol.hpp>
49#endif
50#if defined(OSSIA_PROTOCOL_SERIAL)
51#include <Protocols/Serial/SerialProtocolFactory.hpp>
52#include <Protocols/Serial/SerialSpecificSettings.hpp>
53
54#include <ossia-qt/serial/serial_protocol.hpp>
55#endif
56namespace Protocols
57{
59{
60 SCORE_CONCRETE("8d4c06e2-851b-4d5f-82f2-68056a50c370")
61
62 QSet<QString> acceptedFiles() const noexcept override
63 {
64 return {"touchosc", "json", "xml", "device"};
65 }
66
67 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
68 {
70 bool ok = (path.endsWith(".touchosc") && Device::loadDeviceFromTouchOSC(path, n))
71 || (path.endsWith(".json") && Device::loadDeviceFromScoreJSON(path, n))
72 || (path.endsWith(".xml") && Device::loadDeviceFromXML(path, n))
73 || (path.endsWith(".device") && Device::loadDeviceFromScoreJSON(path, n));
74 if(!ok)
75 return false;
76
77 auto& devplug = ctx.plugin<Explorer::DeviceDocumentPlugin>();
78 auto& model = devplug.explorer();
79
81 if(set.name.isEmpty())
82 set.name = QFileInfo(path).completeBaseName();
83
84 if(set.protocol.impl().is_nil())
85 set.protocol = Protocols::OSCProtocolFactory::static_concreteKey();
86
87 model.checkAndLoadDevice(std::move(n));
88 return true;
89 }
90};
91
92#if __has_include(<QQmlEngine>)
93class QMLLibraryHandler final : public Library::LibraryInterface
94{
95 SCORE_CONCRETE("fee42cea-ff1a-48ef-a0da-922773081779")
96
97 QSet<QString> acceptedFiles() const noexcept override { return {"qml"}; }
98
99 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
100 {
101 QFile f(path);
102 if(!f.open(QIODevice::ReadOnly))
103 return true;
104 auto content = f.readAll();
105
106 QQmlEngine e;
107 QQmlComponent c{&e};
108 c.setData(content, QUrl());
109
110 std::unique_ptr<QObject> obj{c.create()};
111
112 if(!obj)
113 return true;
114
116 set.name = QFileInfo(f).baseName();
117
118 if(dynamic_cast<Protocols::Mapper*>(obj.get()))
119 {
120 set.protocol = Protocols::MapperProtocolFactory::static_concreteKey();
121 set.deviceSpecificSettings
122 = QVariant::fromValue(Protocols::MapperSpecificSettings{content});
123 }
124#if defined(OSSIA_PROTOCOL_SERIAL)
125 else if(dynamic_cast<ossia::net::Serial*>(obj.get()))
126 {
127 set.protocol = Protocols::SerialProtocolFactory::static_concreteKey();
128 set.deviceSpecificSettings
129 = QVariant::fromValue(Protocols::SerialSpecificSettings{{}, content});
130 }
131#endif
132#if defined(OSSIA_PROTOCOL_HTTP)
133 else if(dynamic_cast<ossia::net::HTTP*>(obj.get()))
134 {
135 set.protocol = Protocols::HTTPProtocolFactory::static_concreteKey();
136 set.deviceSpecificSettings
137 = QVariant::fromValue(Protocols::HTTPSpecificSettings{content});
138 }
139#endif
140#if defined(OSSIA_PROTOCOL_WEBSOCKETS)
141 else if(dynamic_cast<ossia::net::WS*>(obj.get()))
142 {
143 set.protocol = Protocols::WSProtocolFactory::static_concreteKey();
144 set.deviceSpecificSettings = QVariant::fromValue(Protocols::WSSpecificSettings{
145 QQmlProperty(obj.get(), "host").read().toString(), content});
146 }
147#endif
148
149 if(set.protocol == UuidKey<Device::ProtocolFactory>{})
150 return false;
151
152 auto& devplug = ctx.plugin<Explorer::DeviceDocumentPlugin>();
153 auto& model = devplug.explorer();
154
155 model.checkAndLoadDevice(set);
156 return true;
157 }
158};
159#endif
160}
Serialization mechanism for ossia::any.
Definition LibraryInterface.hpp:22
Definition ProtocolLibrary.hpp:59
Definition UuidKey.hpp:343
bool loadDeviceFromXML(const QString &filePath, Device::Node &node)
loadDeviceFromJamomaJSON Will load a device in a Jamoma-format XML file into the node.
Definition JamomaDeviceLoader.cpp:248
Definition DeviceSettings.hpp:20
Definition HTTPSpecificSettings.hpp:9
Definition SerialSpecificSettings.hpp:13
Definition WSSpecificSettings.hpp:9
Definition DocumentContext.hpp:18