OBSBasic_Dropfiles.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. #include <qt-wrappers.hpp>
  18. #include <QFileInfo>
  19. #include <QMimeData>
  20. #ifdef _WIN32
  21. #include <QSettings>
  22. #endif
  23. #include <QUrlQuery>
  24. using namespace std;
  25. static const char *textExtensions[] = {"txt", "log", nullptr};
  26. static const char *imageExtensions[] = {"bmp", "gif", "jpeg", "jpg",
  27. #ifdef _WIN32
  28. "jxr",
  29. #endif
  30. "png", "tga", "webp", nullptr};
  31. static const char *htmlExtensions[] = {"htm", "html", nullptr};
  32. static const char *mediaExtensions[] = {
  33. "3ga", "669", "a52", "aac", "ac3", "adt", "adts", "aif", "aifc", "aiff", "amb", "amr", "aob", "ape",
  34. "au", "awb", "caf", "dts", "flac", "it", "kar", "m4a", "m4b", "m4p", "m5p", "mid", "mka", "mlp",
  35. "mod", "mpa", "mp1", "mp2", "mp3", "mpc", "mpga", "mus", "oga", "ogg", "oma", "opus", "qcp", "ra",
  36. "rmi", "s3m", "sid", "spx", "tak", "thd", "tta", "voc", "vqf", "w64", "wav", "wma", "wv", "xa",
  37. "xm", "3g2", "3gp", "3gp2", "3gpp", "amv", "asf", "avi", "bik", "crf", "divx", "drc", "dv", "evo",
  38. "f4v", "flv", "gvi", "gxf", "iso", "m1v", "m2v", "m2t", "m2ts", "m4v", "mkv", "mov", "mp2", "mp2v",
  39. "mp4", "mp4v", "mpe", "mpeg", "mpeg1", "mpeg2", "mpeg4", "mpg", "mpv2", "mts", "mtv", "mxf", "mxg", "nsv",
  40. "nuv", "ogg", "ogm", "ogv", "ogx", "ps", "rec", "rm", "rmvb", "rpl", "thp", "tod", "ts", "tts",
  41. "txd", "vob", "vro", "webm", "wm", "wmv", "wtv", nullptr};
  42. static string GenerateSourceName(const char *base)
  43. {
  44. string name;
  45. int inc = 0;
  46. for (;; inc++) {
  47. name = base;
  48. if (inc) {
  49. name += " (";
  50. name += to_string(inc + 1);
  51. name += ")";
  52. }
  53. OBSSourceAutoRelease source = obs_get_source_by_name(name.c_str());
  54. if (!source)
  55. return name;
  56. }
  57. }
  58. #ifdef _WIN32
  59. static QString ReadWindowsURLFile(const QString &file)
  60. {
  61. QSettings iniFile(file, QSettings::IniFormat);
  62. QVariant url = iniFile.value("InternetShortcut/URL");
  63. return url.toString();
  64. }
  65. #endif
  66. void OBSBasic::AddDropURL(QUrl url, QString &name, obs_data_t *settings, const obs_video_info &ovi)
  67. {
  68. QUrlQuery query = QUrlQuery(url.query(QUrl::FullyEncoded));
  69. int cx = (int)ovi.base_width;
  70. int cy = (int)ovi.base_height;
  71. if (query.hasQueryItem("layer-width"))
  72. cx = query.queryItemValue("layer-width").toInt();
  73. if (query.hasQueryItem("layer-height"))
  74. cy = query.queryItemValue("layer-height").toInt();
  75. if (query.hasQueryItem("layer-css")) {
  76. // QUrl::FullyDecoded does NOT properly decode a
  77. // application/x-www-form-urlencoded space represented as '+'
  78. // Thus, this is manually filtered out before QUrl's
  79. // decoding kicks in again. This is to allow JavaScript's
  80. // default searchParams.append function to simply append css
  81. // to the query parameters, which is the intended usecase for this.
  82. QString fullyEncoded = query.queryItemValue("layer-css", QUrl::FullyEncoded);
  83. fullyEncoded = fullyEncoded.replace("+", "%20");
  84. QString decoded = QUrl::fromPercentEncoding(QByteArray::fromStdString(QT_TO_UTF8(fullyEncoded)));
  85. obs_data_set_string(settings, "css", QT_TO_UTF8(decoded));
  86. }
  87. obs_data_set_int(settings, "width", cx);
  88. obs_data_set_int(settings, "height", cy);
  89. name = query.hasQueryItem("layer-name") ? query.queryItemValue("layer-name", QUrl::FullyDecoded) : url.host();
  90. query.removeQueryItem("layer-width");
  91. query.removeQueryItem("layer-height");
  92. query.removeQueryItem("layer-name");
  93. query.removeQueryItem("layer-css");
  94. url.setQuery(query);
  95. obs_data_set_string(settings, "url", QT_TO_UTF8(url.url()));
  96. }
  97. void OBSBasic::AddDropSource(const char *data, DropType image)
  98. {
  99. OBSBasic *main = OBSBasic::Get();
  100. OBSDataAutoRelease settings = obs_data_create();
  101. const char *type = nullptr;
  102. QString name;
  103. obs_video_info ovi;
  104. obs_get_video_info(&ovi);
  105. switch (image) {
  106. case DropType_RawText:
  107. obs_data_set_string(settings, "text", data);
  108. #ifdef _WIN32
  109. type = "text_gdiplus";
  110. #else
  111. type = "text_ft2_source";
  112. #endif
  113. break;
  114. case DropType_Text:
  115. #ifdef _WIN32
  116. obs_data_set_bool(settings, "read_from_file", true);
  117. obs_data_set_string(settings, "file", data);
  118. name = QUrl::fromLocalFile(QString(data)).fileName();
  119. type = "text_gdiplus";
  120. #else
  121. obs_data_set_bool(settings, "from_file", true);
  122. obs_data_set_string(settings, "text_file", data);
  123. type = "text_ft2_source";
  124. #endif
  125. break;
  126. case DropType_Image:
  127. obs_data_set_string(settings, "file", data);
  128. name = QUrl::fromLocalFile(QString(data)).fileName();
  129. type = "image_source";
  130. break;
  131. case DropType_Media:
  132. obs_data_set_string(settings, "local_file", data);
  133. name = QUrl::fromLocalFile(QString(data)).fileName();
  134. type = "ffmpeg_source";
  135. break;
  136. case DropType_Html:
  137. obs_data_set_bool(settings, "is_local_file", true);
  138. obs_data_set_string(settings, "local_file", data);
  139. obs_data_set_int(settings, "width", ovi.base_width);
  140. obs_data_set_int(settings, "height", ovi.base_height);
  141. name = QUrl::fromLocalFile(QString(data)).fileName();
  142. type = "browser_source";
  143. break;
  144. case DropType_Url:
  145. AddDropURL(QUrl(data), name, settings, ovi);
  146. type = "browser_source";
  147. break;
  148. }
  149. type = obs_get_latest_input_type_id(type);
  150. if (type == nullptr || !obs_source_get_display_name(type)) {
  151. return;
  152. }
  153. if (name.isEmpty())
  154. name = obs_source_get_display_name(type);
  155. std::string sourceName = GenerateSourceName(QT_TO_UTF8(name));
  156. OBSSourceAutoRelease source = obs_source_create(type, sourceName.c_str(), settings, nullptr);
  157. if (source) {
  158. OBSDataAutoRelease wrapper = obs_save_source(source);
  159. OBSScene scene = main->GetCurrentScene();
  160. std::string sceneUUID = obs_source_get_uuid(obs_scene_get_source(scene));
  161. std::string sourceUUID = obs_source_get_uuid(source);
  162. auto undo = [sceneUUID, sourceUUID](const std::string &) {
  163. OBSSourceAutoRelease source = obs_get_source_by_uuid(sourceUUID.c_str());
  164. obs_source_remove(source);
  165. OBSSourceAutoRelease scene = obs_get_source_by_uuid(sceneUUID.c_str());
  166. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  167. };
  168. auto redo = [sceneUUID, sourceName, type](const std::string &data) {
  169. OBSSourceAutoRelease scene = obs_get_source_by_uuid(sceneUUID.c_str());
  170. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  171. OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str());
  172. OBSSourceAutoRelease source = obs_load_source(dat);
  173. obs_scene_add(obs_scene_from_source(scene), source.Get());
  174. };
  175. undo_s.add_action(QTStr("Undo.Add").arg(sourceName.c_str()), undo, redo, "",
  176. std::string(obs_data_get_json(wrapper)));
  177. obs_scene_add(scene, source);
  178. }
  179. }
  180. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  181. {
  182. if (event->mimeData()->hasFormat("application/x-obs-source-uuid")) {
  183. event->acceptProposedAction();
  184. }
  185. // refuse drops of our own widgets
  186. if (event->source() != nullptr) {
  187. event->setDropAction(Qt::IgnoreAction);
  188. return;
  189. }
  190. event->acceptProposedAction();
  191. }
  192. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  193. {
  194. event->accept();
  195. }
  196. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  197. {
  198. event->acceptProposedAction();
  199. }
  200. void OBSBasic::ConfirmDropUrl(const QString &url)
  201. {
  202. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  203. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  204. activateWindow();
  205. QString msg = QTStr("AddUrl.Text");
  206. msg += "\n\n";
  207. msg += QTStr("AddUrl.Text.Url").arg(url);
  208. QMessageBox messageBox(this);
  209. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  210. messageBox.setText(msg);
  211. QPushButton *yesButton = messageBox.addButton(QTStr("Yes"), QMessageBox::YesRole);
  212. QPushButton *noButton = messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  213. messageBox.setDefaultButton(yesButton);
  214. messageBox.setEscapeButton(noButton);
  215. messageBox.setIcon(QMessageBox::Question);
  216. messageBox.exec();
  217. if (messageBox.clickedButton() == yesButton)
  218. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  219. }
  220. }
  221. void OBSBasic::dropEvent(QDropEvent *event)
  222. {
  223. const QMimeData *mimeData = event->mimeData();
  224. if (mimeData->hasUrls()) {
  225. QList<QUrl> urls = mimeData->urls();
  226. for (int i = 0; i < urls.size(); i++) {
  227. QUrl url = urls[i];
  228. QString file = url.toLocalFile();
  229. QFileInfo fileInfo(file);
  230. if (!fileInfo.exists()) {
  231. ConfirmDropUrl(url.url());
  232. continue;
  233. }
  234. #ifdef _WIN32
  235. if (fileInfo.suffix().compare("url", Qt::CaseInsensitive) == 0) {
  236. QString urlTarget = ReadWindowsURLFile(file);
  237. if (!urlTarget.isEmpty()) {
  238. ConfirmDropUrl(urlTarget);
  239. }
  240. continue;
  241. } else if (fileInfo.isShortcut()) {
  242. file = fileInfo.symLinkTarget();
  243. fileInfo = QFileInfo(file);
  244. if (!fileInfo.exists()) {
  245. continue;
  246. }
  247. }
  248. #endif
  249. QString suffixQStr = fileInfo.suffix();
  250. QByteArray suffixArray = suffixQStr.toUtf8();
  251. const char *suffix = suffixArray.constData();
  252. bool found = false;
  253. const char **cmp;
  254. #define CHECK_SUFFIX(extensions, type) \
  255. cmp = extensions; \
  256. while (*cmp) { \
  257. if (astrcmpi(*cmp, suffix) == 0) { \
  258. AddDropSource(QT_TO_UTF8(file), type); \
  259. found = true; \
  260. break; \
  261. } \
  262. \
  263. cmp++; \
  264. } \
  265. \
  266. if (found) \
  267. continue;
  268. CHECK_SUFFIX(textExtensions, DropType_Text);
  269. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  270. CHECK_SUFFIX(imageExtensions, DropType_Image);
  271. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  272. #undef CHECK_SUFFIX
  273. }
  274. } else if (mimeData->hasText()) {
  275. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  276. } else if (event->mimeData()->hasFormat("application/x-obs-source-uuid")) {
  277. QString uuid = QString::fromUtf8(event->mimeData()->data("application/x-obs-source-uuid"));
  278. emit sourceUuidDropped(uuid);
  279. event->acceptProposedAction();
  280. }
  281. }