|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Jolla Ltd. |
| 3 | + * Contact: Pekka Vuorela <[email protected]> |
| 4 | + * |
| 5 | + * You may use this file under the terms of the BSD license as follows: |
| 6 | + * |
| 7 | + * "Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are |
| 9 | + * met: |
| 10 | + * * Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * * Neither the name of Nemo Mobile nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this |
| 18 | + * software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 31 | + */ |
| 32 | + |
| 33 | +#include <QFile> |
| 34 | +#include <QRegularExpression> |
| 35 | +#include <QDebug> |
| 36 | + |
| 37 | +#include <stdlib.h> |
| 38 | +#include <unistd.h> |
| 39 | +#include <errno.h> |
| 40 | +#include <string.h> |
| 41 | + |
| 42 | +#include "../src/localeconfig.h" |
| 43 | + |
| 44 | +int main(int argc, char *argv[]) |
| 45 | +{ |
| 46 | + if (argc != 2) { |
| 47 | + qWarning() << "No locale given"; |
| 48 | + return EXIT_FAILURE; |
| 49 | + } |
| 50 | + |
| 51 | + QString configPath = localeConfigPath(); |
| 52 | + |
| 53 | + if (configPath.isEmpty()) { |
| 54 | + return EXIT_FAILURE; |
| 55 | + } |
| 56 | + |
| 57 | + QString newLocale = QString(argv[1]); |
| 58 | + QRegularExpression allowedInput("^[a-zA-Z0-9\\.@_]*$"); |
| 59 | + if (!allowedInput.match(newLocale).hasMatch()) { |
| 60 | + qWarning() << "Invalid locale input:" << newLocale; |
| 61 | + return EXIT_FAILURE; |
| 62 | + } |
| 63 | + |
| 64 | + QFile localeConfig(configPath); |
| 65 | + if (!localeConfig.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { |
| 66 | + qWarning() << "Unable to open locale configuration file for writing:" << configPath |
| 67 | + << "-" << localeConfig.errorString(); |
| 68 | + return EXIT_FAILURE; |
| 69 | + } |
| 70 | + |
| 71 | + localeConfig.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | |
| 72 | + QFileDevice::ReadGroup | QFileDevice::ReadOther); |
| 73 | + |
| 74 | + if (fchown(localeConfig.handle(), 0, 0)) { |
| 75 | + qWarning() << "Failed to set localeconfig as root:root" << strerror(errno); |
| 76 | + } |
| 77 | + |
| 78 | + localeConfig.write("# Autogenerated by settings\n"); |
| 79 | + localeConfig.write(QString("LANG=%1\n").arg(newLocale).toLatin1()); |
| 80 | + localeConfig.close(); |
| 81 | + |
| 82 | + return EXIT_SUCCESS; |
| 83 | +} |
0 commit comments