OBSBasic_Dropfiles.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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(const char *url, QString &name, obs_data_t *settings, const obs_video_info &ovi)
  67. {
  68. QUrl path = QString::fromUtf8(url);
  69. QUrlQuery query = QUrlQuery(path.query(QUrl::FullyEncoded));
  70. int cx = (int)ovi.base_width;
  71. int cy = (int)ovi.base_height;
  72. if (query.hasQueryItem("layer-width"))
  73. cx = query.queryItemValue("layer-width").toInt();
  74. if (query.hasQueryItem("layer-height"))
  75. cy = query.queryItemValue("layer-height").toInt();
  76. if (query.hasQueryItem("layer-css")) {
  77. // QUrl::FullyDecoded does NOT properly decode a
  78. // application/x-www-form-urlencoded space represented as '+'
  79. // Thus, this is manually filtered out before QUrl's
  80. // decoding kicks in again. This is to allow JavaScript's
  81. // default searchParams.append function to simply append css
  82. // to the query parameters, which is the intended usecase for this.
  83. QString fullyEncoded = query.queryItemValue("layer-css", QUrl::FullyEncoded);
  84. fullyEncoded = fullyEncoded.replace("+", "%20");
  85. QString decoded = QUrl::fromPercentEncoding(QByteArray::fromStdString(QT_TO_UTF8(fullyEncoded)));
  86. obs_data_set_string(settings, "css", QT_TO_UTF8(decoded));
  87. }
  88. obs_data_set_int(settings, "width", cx);
  89. obs_data_set_int(settings, "height", cy);
  90. name = query.hasQueryItem("layer-name") ? query.queryItemValue("layer-name", QUrl::FullyDecoded) : path.host();
  91. query.removeQueryItem("layer-width");
  92. query.removeQueryItem("layer-height");
  93. query.removeQueryItem("layer-name");
  94. query.removeQueryItem("layer-css");
  95. path.setQuery(query);
  96. obs_data_set_string(settings, "url", QT_TO_UTF8(path.url()));
  97. }
  98. void OBSBasic::AddDropSource(const char *data, DropType image)
  99. {
  100. OBSBasic *main = OBSBasic::Get();
  101. OBSDataAutoRelease settings = obs_data_create();
  102. const char *type = nullptr;
  103. QString name;
  104. obs_video_info ovi;
  105. obs_get_video_info(&ovi);
  106. switch (image) {
  107. case DropType_RawText:
  108. obs_data_set_string(settings, "text", data);
  109. #ifdef _WIN32
  110. type = "text_gdiplus";
  111. #else
  112. type = "text_ft2_source";
  113. #endif
  114. break;
  115. case DropType_Text:
  116. #ifdef _WIN32
  117. obs_data_set_bool(settings, "read_from_file", true);
  118. obs_data_set_string(settings, "file", data);
  119. name = QUrl::fromLocalFile(QString(data)).fileName();
  120. type = "text_gdiplus";
  121. #else
  122. obs_data_set_bool(settings, "from_file", true);
  123. obs_data_set_string(settings, "text_file", data);
  124. type = "text_ft2_source";
  125. #endif
  126. break;
  127. case DropType_Image:
  128. obs_data_set_string(settings, "file", data);
  129. name = QUrl::fromLocalFile(QString(data)).fileName();
  130. type = "image_source";
  131. break;
  132. case DropType_Media:
  133. obs_data_set_string(settings, "local_file", data);
  134. name = QUrl::fromLocalFile(QString(data)).fileName();
  135. type = "ffmpeg_source";
  136. break;
  137. case DropType_Html:
  138. obs_data_set_bool(settings, "is_local_file", true);
  139. obs_data_set_string(settings, "local_file", data);
  140. obs_data_set_int(settings, "width", ovi.base_width);
  141. obs_data_set_int(settings, "height", ovi.base_height);
  142. name = QUrl::fromLocalFile(QString(data)).fileName();
  143. type = "browser_source";
  144. break;
  145. case DropType_Url:
  146. AddDropURL(data, name, settings, ovi);
  147. type = "browser_source";
  148. break;
  149. }
  150. type = obs_get_latest_input_type_id(type);
  151. if (type == nullptr || !obs_source_get_display_name(type)) {
  152. return;
  153. }
  154. if (name.isEmpty())
  155. name = obs_source_get_display_name(type);
  156. std::string sourceName = GenerateSourceName(QT_TO_UTF8(name));
  157. OBSSourceAutoRelease source = obs_source_create(type, sourceName.c_str(), settings, nullptr);
  158. if (source) {
  159. OBSDataAutoRelease wrapper = obs_save_source(source);
  160. OBSScene scene = main->GetCurrentScene();
  161. std::string sceneUUID = obs_source_get_uuid(obs_scene_get_source(scene));
  162. std::string sourceUUID = obs_source_get_uuid(source);
  163. auto undo = [sceneUUID, sourceUUID](const std::string &) {
  164. OBSSourceAutoRelease source = obs_get_source_by_uuid(sourceUUID.c_str());
  165. obs_source_remove(source);
  166. OBSSourceAutoRelease scene = obs_get_source_by_uuid(sceneUUID.c_str());
  167. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  168. };
  169. auto redo = [sceneUUID, sourceName, type](const std::string &data) {
  170. OBSSourceAutoRelease scene = obs_get_source_by_uuid(sceneUUID.c_str());
  171. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  172. OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str());
  173. OBSSourceAutoRelease source = obs_load_source(dat);
  174. obs_scene_add(obs_scene_from_source(scene), source.Get());
  175. };
  176. undo_s.add_action(QTStr("Undo.Add").arg(sourceName.c_str()), undo, redo, "",
  177. std::string(obs_data_get_json(wrapper)));
  178. obs_scene_add(scene, source);
  179. }
  180. }
  181. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  182. {
  183. // refuse drops of our own widgets
  184. if (event->source() != nullptr) {
  185. event->setDropAction(Qt::IgnoreAction);
  186. return;
  187. }
  188. event->acceptProposedAction();
  189. }
  190. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  191. {
  192. event->accept();
  193. }
  194. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  195. {
  196. event->acceptProposedAction();
  197. }
  198. void OBSBasic::ConfirmDropUrl(const QString &url)
  199. {
  200. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  201. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  202. activateWindow();
  203. QString msg = QTStr("AddUrl.Text");
  204. msg += "\n\n";
  205. msg += QTStr("AddUrl.Text.Url").arg(url);
  206. QMessageBox messageBox(this);
  207. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  208. messageBox.setText(msg);
  209. QPushButton *yesButton = messageBox.addButton(QTStr("Yes"), QMessageBox::YesRole);
  210. QPushButton *noButton = messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  211. messageBox.setDefaultButton(yesButton);
  212. messageBox.setEscapeButton(noButton);
  213. messageBox.setIcon(QMessageBox::Question);
  214. messageBox.exec();
  215. if (messageBox.clickedButton() == yesButton)
  216. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  217. }
  218. }
  219. void OBSBasic::dropEvent(QDropEvent *event)
  220. {
  221. const QMimeData *mimeData = event->mimeData();
  222. if (mimeData->hasUrls()) {
  223. QList<QUrl> urls = mimeData->urls();
  224. for (int i = 0; i < urls.size(); i++) {
  225. QUrl url = urls[i];
  226. QString file = url.toLocalFile();
  227. QFileInfo fileInfo(file);
  228. if (!fileInfo.exists()) {
  229. ConfirmDropUrl(url.url());
  230. continue;
  231. }
  232. #ifdef _WIN32
  233. if (fileInfo.suffix().compare("url", Qt::CaseInsensitive) == 0) {
  234. QString urlTarget = ReadWindowsURLFile(file);
  235. if (!urlTarget.isEmpty()) {
  236. ConfirmDropUrl(urlTarget);
  237. }
  238. continue;
  239. } else if (fileInfo.isShortcut()) {
  240. file = fileInfo.symLinkTarget();
  241. fileInfo = QFileInfo(file);
  242. if (!fileInfo.exists()) {
  243. continue;
  244. }
  245. }
  246. #endif
  247. QString suffixQStr = fileInfo.suffix();
  248. QByteArray suffixArray = suffixQStr.toUtf8();
  249. const char *suffix = suffixArray.constData();
  250. bool found = false;
  251. const char **cmp;
  252. #define CHECK_SUFFIX(extensions, type) \
  253. cmp = extensions; \
  254. while (*cmp) { \
  255. if (astrcmpi(*cmp, suffix) == 0) { \
  256. AddDropSource(QT_TO_UTF8(file), type); \
  257. found = true; \
  258. break; \
  259. } \
  260. \
  261. cmp++; \
  262. } \
  263. \
  264. if (found) \
  265. continue;
  266. CHECK_SUFFIX(textExtensions, DropType_Text);
  267. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  268. CHECK_SUFFIX(imageExtensions, DropType_Image);
  269. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  270. #undef CHECK_SUFFIX
  271. }
  272. } else if (mimeData->hasText()) {
  273. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  274. }
  275. }